-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignCookies.java
More file actions
51 lines (46 loc) · 1.44 KB
/
AssignCookies.java
File metadata and controls
51 lines (46 loc) · 1.44 KB
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
47
48
49
50
51
package leetcode;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* AssignCookies
* https://leetcode-cn.com/problems/assign-cookies/
* 455. 分发饼干
*
* @since 2020-12-25
*/
public class AssignCookies {
public static void main(String[] args) {
AssignCookies sol = new AssignCookies();
System.out.println(sol.findContentChildren(new int[]{1, 2, 3}, new int[]{1, 1}));
System.out.println(sol.findContentChildren(new int[]{1, 2}, new int[]{1, 2, 3}));
}
public int findContentChildren(int[] g, int[] s) {
List<Integer> boys = new LinkedList<>();
for (int i : g) {
boys.add(i);
}
Collections.sort(boys);
List<Integer> cookies = new LinkedList<>();
for (int i : s) {
cookies.add(i);
}
Collections.sort(cookies);
int total = 0;
boolean[] cookieUsed = new boolean[s.length];
int j = 0;
for (int i = 0; i < g.length; i++) {
int requirement = boys.get(i);
//for (int j = 0; j < s.length; j++) { // 不必循环,如果前面都不满足,也不用遍历了
while (j < s.length) {
if (!cookieUsed[j] && cookies.get(j) >= requirement) {
cookieUsed[j] = true;
total++;
break;
}
j++;
}
}
return total;
}
}