Menu

Optimizing Generosity: A Greedy Approach to LeetCode 455 (Assign Cookies)
🖼️

Optimizing Generosity: A Greedy Approach to LeetCode 455 (Assign Cookies)

DEV Community·Jerin·6 months ago
#cbMRn9Th
#java#example#algorithm#leetcode#child
Reading 0:00
15s threshold

Difficulty : Easy

Topics : Array, Two Pointers, Greedy, Sorting

Platform : Leetcode

Problem Statement

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.

Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

Problem Statement Simplified

Give each children a cookies and each children have a dreed factor, give the result of how many children were satisfied.

Mistakes and Learning

Dont check for eact value - in greedy problems we should look for threhold not exact value.

At first used the 2 lists that stored the 2 arrays then remove the one by one from lists after going through the loop - The problem : If remove an element from index 2, then the…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.