leetcode_455

Assign Cookies


Problem

题目太长了

Solution

第一种思路是遍历孩子的那个数组

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
public class {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int count = 0;
int j = 0;
for(int i = 0; i < g.length; i++) {
while(j < s.length && g[i] > s[j]){
j++;
}
if( j < s.length) {
count++;
j++;
} else {
break;
}
}
return count;
}
}

第二种思路是遍历饼干数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int i = 0;
for(int j = 0; i < g.length && j < s.length; j++) {
// 以一块饼干,去匹配孩子
// 如果当前这个孩子可以匹配,那下一轮可以匹配下一个孩子(i++)
// 不匹配的话,到下一轮循环去看,下一块饼干去匹配
if(g[i] <= s[j]){
i++;
}
}
return i;
}
}