오늘의 문제
easy난이도, greedy 문제
의식의 흐름
각 배열을 정렬시키고 해당 원소 값이 동일하면 나오지 않을까?
import java.util.Arrays; class Solution { public int findContentChildren(int[] g, int[] s) { int smallArrSize = 0; int count = 0; int gSize = g.length; int sSize = s.length; Arrays.sort(g); Arrays.sort(s); smallArrSize = (sSize < gSize) ? sSize : gSize; for(int i = 0; i < smallArrSize; i++) { if(g[i] == s[i]) count ++; } return count; } } [1,2,3] [3]
대소비교?
import java.util.Arrays; import java.util.stream.IntStream; class Solution { public int findContentChildren(int[] g, int[] s) { int smallArrSize = 0; int count = 0; int gSize = g.length; int sSize = s.length; Arrays.sort(g); Arrays.sort(s); smallArrSize = (sSize < gSize) ? sSize : gSize; for(int i = 0; i < smallArrSize; i++) { if(s[i] - g[i] >= 0) count++; } return count; } } [10,9,8,7] [5,6,7,8]
Arrays.asList()contains()를 쓰면 되겠지? -> false가 나온다 아마 Integer로 비교해야 할 것 같다.
// 아마 이런 비교가 아니었을까? int i = 1; int[] a= {i}; System.out.println(a.equals(i)); Returns a fixed-size list backed by the specified array. (Changes tothe returned list "write through" to the array.) This method actsas bridge between array-based and collection-based APIs, incombination with Collection.toArray(). The returned list isserializable and implements RandomAccess. This method also provides a convenient way to create a fixed-sizelist initialized to contain several elements: List<String> stooges = Arrays.asList("Larry", "Moe", "Curly"); contains boolean contains(Object o) Returns true if this list contains the specified element.More formally, returns true if and only if this list containsat least one element e such that (o==null ? e==null : o.equals(e)). Specified by:contains in interface Collection<E>Parameters:o - element whose presence in this list is to be testedReturns:true if this list contains the specified elementThrows:ClassCastException - if the type of the specified elementis incompatible with this list(optional)NullPointerException - if the specified element is null and thislist does not permit null elements(optional)
정확한 이유는 나중에 다시 알아보자.
import java.util.Arrays; class Solution { public int findContentChildren(int[] g, int[] s) { // 정렬 Arrays.sort(g); Arrays.sort(s); int count = 0; // 가장 작은 쿠키부터 아이들 욕심과 비교하여 분배 for(int sIdx = 0; count < g.length && sIdx < s.length; sIdx++) { if(g[count]<=s[sIdx]) count++; } return count; } }
메모리 상위권 코드
class Solution { public int findContentChildren(int[] g, int[] s) { int res = 0; ArrayList<Integer> cookies = new ArrayList<>(); for (int i = 0; i < s.length; i++) { cookies.add(new Integer(s[i])); } Collections.sort(cookies); for(int i=0;i<g.length;i++){ if(cookies.isEmpty()){ return res; } if(eatCookie(g[i],cookies)){ res++; } } return res; } public boolean eatCookie(int g, List<Integer> cookies) { boolean res = false; for(int i=0;i<cookies.size();i++){ if(cookies.get(i).intValue() >= g){ cookies.remove(i); return true; } } return res; } }
leetcode.com/problems/assign-cookies/
Assign Cookies - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
'Algorithm > LeetCode' 카테고리의 다른 글
[LeetCode] Divisor Game (0) | 2021.03.08 |
---|---|
[LeetCode] Fibonacci number (피보나치 수열) (0) | 2021.03.06 |
[LeetCode] Sort Colors (0) | 2021.03.05 |
[LeetCode] Make The String Great (0) | 2021.03.04 |
[LeetCode] Palindrome Number (대칭수) (0) | 2021.03.02 |