Skip to content

Commit 00b4de4

Browse files
committed
Add solution to leetcode 122
1 parent b198a9a commit 00b4de4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Link to problem: https://leetcode.com/problems/assign-cookies/
2+
3+
def findContentChildren(g, s):
4+
# Sort the children and the cookies in increasing order
5+
g.sort() # Children greed factors
6+
s.sort() # Cookie sizes
7+
8+
content = 0 # Number of content children
9+
i,j = 0,0 # i: index in g, j: index in s
10+
num_child, num_cook = len(g), len(s)
11+
12+
while i < num_child and j < num_cook:
13+
curr_child = g[i]
14+
curr_cook = s[j]
15+
# Current child content with the current cookie
16+
if curr_child <= curr_cook:
17+
content += 1
18+
i += 1
19+
j += 1
20+
else:
21+
# Current child too greedy to be content w/ current cookie
22+
j += 1 # Try out a larger cookie
23+
24+
return content

0 commit comments

Comments
 (0)