forked from liuyubobobo/Play-with-Algorithm-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
38 lines (30 loc) · 912 Bytes
/
Solution.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
import java.util.Arrays;
/// 455. Assign Cookies
/// https://leetcode.com/problems/assign-cookies/description/
/// 先尝试满足最贪心的小朋友
/// 时间复杂度: O(nlogn)
/// 空间复杂度: O(1)
public class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int gi = g.length - 1, si = s.length - 1;
int res = 0;
while(gi >= 0 && si >= 0){
if(s[si] >= g[gi]){
res ++;
si --;
}
gi --;
}
return res;
}
public static void main(String[] args) {
int g1[] = {1, 2, 3};
int s1[] = {1, 1};
System.out.println((new Solution()).findContentChildren(g1, s1));
int g2[] = {1, 2};
int s2[] = {1, 2, 3};
System.out.println((new Solution()).findContentChildren(g2, s2));
}
}