Assign Cookies
LeetCode 455 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Sort both; smallest sufficient cookie per child.
நான் Assign Cookies (LeetCode 455) ஐ இப்படித் தீர்த்தேன் — Sorting + Simple Greedy பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Sort children by greed and cookies by size. Give each child the smallest cookie that satisfies them — using a bigger cookie than needed can only hurt a later, greedier child.
Approach
- 1
Sort g and s.
- 2
If the current cookie satisfies the current child, count it and move to the next child.
- 3
Always move to the next cookie.
நான் எப்படித் தீர்த்தேன்
Greedy two pointersPythonO(n log n) time
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
student=0
i=0
j=0
while i<len(g) and j<len(s):
if s[j]>=g[i]:
student+=1
i+=1
j+=1
return student
Assign Cookies — அடிக்கடி கேட்கப்படும் கேள்விகள்
Assign Cookies பின்னாலுள்ள intuition என்ன?
Sort children by greed and cookies by size. Give each child the smallest cookie that satisfies them — using a bigger cookie than needed can only hurt a later, greedier child.
Assign Cookies (LeetCode 455) ஐ எப்படித் தீர்ப்பது?
1. Sort g and s. 2. If the current cookie satisfies the current child, count it and move to the next child. 3. Always move to the next cookie.
Assign Cookies இன் time மற்றும் space complexity என்ன?
Time: O(n log n + m log m). Space: O(1). (Accepted approach: Greedy two pointers.)
Assign Cookies இல் பொதுவான தவறுகள் என்ன?
Skip cookies that are too small rather than skipping children.