-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAssignCookies.java
46 lines (44 loc) · 1.16 KB
/
AssignCookies.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*https://leetcode.com/problems/assign-cookies/*/
class Solution {
public int findContentChildren(int[] g, int[] s) {
PriorityQueue<Integer> children = new PriorityQueue<>(), cookies = new PriorityQueue<>();
for (int child : g)
children.add(child);
for (int cookie : s)
cookies.add(cookie);
int contentChildren = 0;
while (!children.isEmpty())
{
int child = children.poll();
while (!cookies.isEmpty())
{
int cookie = cookies.poll();
if (cookie >= child)
{
++contentChildren;
break;
}
}
}
return contentChildren;
}
}
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(s);
Arrays.sort(g);
int count = 0;
int i = 0, j = 0;
if (s.length == 0) return 0;
while (i < s.length && j < g.length)
{
if (s[i] >= g[j])
{
++count;
++j;
}
++i;
}
return count;
}
}