Skip to content

Commit b6751aa

Browse files
committed
https://leetcode.com/problems/assign-cookies/
1 parent 4d9437e commit b6751aa

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

EasyProblems/AssignCookies.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace LeetCode.EasyProblems;
2+
3+
/// <summary>
4+
/// https://leetcode.com/problems/assign-cookies/
5+
/// </summary>
6+
public class AssignCookies
7+
{
8+
public int FindContentChildren(int[] greed, int[] cookieSize)
9+
{
10+
// Sort the greed factors and cookie sizes in ascending order
11+
Array.Sort(greed); // O(n log n), where n = greed.Length
12+
Array.Sort(cookieSize); // O(m log m), where m = cookieSize.Length
13+
14+
int child = 0; // Pointer for greed array (children)
15+
int cookie = 0; // Pointer for cookieSize array (cookies)
16+
int count = 0; // Number of content children
17+
18+
// Try to satisfy each child with the smallest sufficient cookie
19+
while (child < greed.Length && cookie < cookieSize.Length)
20+
{
21+
if (greed[child] <= cookieSize[cookie])
22+
{
23+
// If the current cookie can satisfy the current child
24+
count++; // Increment content children count
25+
child++; // Move to next child
26+
}
27+
// Move to next cookie in either case
28+
cookie++;
29+
}
30+
return count;
31+
}
32+
}

0 commit comments

Comments
 (0)