-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssign Cookies.py
31 lines (23 loc) · 1.06 KB
/
Assign Cookies.py
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
# https://leetcode.com/problems/assign-cookies/
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
"""
g[] => Each element represents a child and how much of a cookie they need to be satisfied
s[] => Each element represents a cookie and how big they are
"""
# Sort both lists (order doesn't matter in this question)
g.sort()
s.sort()
# Create a variable to hold number of satisfied eaters
satisfied = 0
# Iterate cookies list
for idx in range(len(s)):
# If there aren't any more kids, exit
if satisfied >= len(g):
break
# If kid can be fed with current cookie, increment counter
if s[idx] >= g[satisfied]:
satisfied += 1
# Return total of satisfied eaters
# NOTE: "satisfied" doesn't have to reach end of list. If not, then other kids won't get to eat a cookie
return satisfied