-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassign_cookies.py
More file actions
34 lines (26 loc) · 898 Bytes
/
assign_cookies.py
File metadata and controls
34 lines (26 loc) · 898 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
31
32
33
34
class Solution:
def findContentChildren(self, greed, cookie):
happy = 0
i, j = 0, 0
greed.sort() # greed factor
cookie.sort() # cookie size
while i < len(greed) and j < len(cookie):
if cookie[j] >= greed[i]:
happy += 1
i += 1
# cannot satisfy
# move cookie size forward
# find another cookie that satisfies the child locally
# each step should be optimised locally, without considering consequences later
j += 1
return happy
# Time Complexity: O(n log n + m log m)
# Space Complexity: O(1)
if __name__ == "__main__":
solution = Solution()
greed = [1, 2, 3]
cookie = [1, 1]
result = solution.findContentChildren(greed, cookie)
assert result == 1
print(result)
print("Test Case 1 Passed!")