Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b198a9a

Browse files
Add solution to leetcode 455
1 parent c3bc6ef commit b198a9a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎455-Assign Cookies.py

Lines changed: 24 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /