-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assign_cookies.java
49 lines (46 loc) · 1.24 KB
/
Assign_cookies.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
47
48
49
// 455 - Assign Cookies
import java.util.Arrays;
public class Assign_cookies {
public static void main(String[] args) {
int [] g = {1,2,3,9,10,2,3,5};
int [] s = {3,2,1,6,4,6};
System.out.println(findContentChildren(g, s));
}
public static int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int greed = 0;
// for(int i = 0;i<g.length;i++){
// for(int j = 0;j<s.length;j++){
// if(g[i]<=s[j]){
// greed++;
// s[j] = 0;
// break;
// }
// }
// }
//optimised code
// int i = 0;
// int j = 0;
// while(i<g.length && j<s.length){
// if(g[i]<=s[j]){
// greed++;
// s[j] = 0;
// i++;
// }
// j++;
// }
//more optimised
int i = g.length - 1;
int j = s.length - 1;
while(i>=0 && j>=0){
if(g[i]<=s[j]){
greed++;
s[j] = 0;
j--;
}
i--;
}
return greed;
}
}