Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- 트리
- list
- Bit
- Math
- Medium
- LinkedList
- sorting
- 리스트
- Subset
- Easy
- tree
- traversal
- Bubble Sort
- kakao
- 카카오
- leetcode
- stack
- 오픈 채팅방
- 공채
- 완전검색
- Linked List
- 배열
- 정렬
- 스택
- 순회
- bst
- 2019 카카오
- binary search tree
Archives
- Today
- Total
미리수얌 블로그
Leetcode: Assign Cookies 본문
Assign Cookies
학생이 원하는 쿠키의 크기와 가지고 있는 쿠키들의 크기들이 배열로 주어졌을때 최대한 나눠 줄수 있는 학생의 수를 구하여라
예 학생: [2, 3, 1] 쿠키 [1, 1, 1] 쿠키가 3개나 있지만 크기는 한명 밖에 만족을 할 수 없으므로 답은 1.
Greed Algorithm 을 쓸 수 있습니다. 제일 작은 쿠키 부터 하나씩 학생에게 나누어 주면 됩니다.
public int findContentChildren(int[] g, int[] s) {
// greedy
Arrays.sort(g);
Arrays.sort(s);
int sumCount = 0;
int gi = 0;
int si = 0;
while (gi < g.length && si < s.length) {
if (g[gi] <= s[si]) {
gi++;
}
si++;
}
return gi;
}
'코딩문제풀이 > Leetcode' 카테고리의 다른 글
Leetcode: Diameter of Binary Tree (0) | 2018.09.29 |
---|---|
Leetcode: String Compression (0) | 2018.09.29 |
LeetCode: Kth Smallest Element in a BST (0) | 2018.09.29 |
Leetcode: Reverse Words in a String III (0) | 2018.09.29 |
LeetCode: Count Numbers with Unique Digits (0) | 2018.09.29 |
Comments