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.