-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssign Cookies.java
More file actions
30 lines (22 loc) · 986 Bytes
/
Assign Cookies.java
File metadata and controls
30 lines (22 loc) · 986 Bytes
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
// https://leetcode.com/problems/assign-cookies/
class Solution {
public int findContentChildren(int[] g, int[] s) {
// If there aren't any cookies, return 0
if (s.length == 0) {return 0;}
// Sort both arrays
Arrays.sort(g);
Arrays.sort(s);
// Create a variable to hold number of satisfied eaters
int satisfied = 0;
// Iterate cookie array
for (int cookieSize: s) {
// If number of satisfiers exceed cookie eater size, exit loop
if (satisfied >= g.length) {break;}
// If cookie can satisfy eater, increment counter
if (cookieSize >= g[satisfied]) {satisfied++;}
}
// Return the number of satisfied cookie eaters
// NOTE: "satisfied" doesn't have to reach end of array. It just means that the other kids couldn't get to eat a cookie
return satisfied;
}
}