Skip to content

Commit 021a47e

Browse files
committed
Sync LeetCode submission Runtime - 295 ms (6.65%), Memory - 19.3 MB (13.07%)
1 parent 6661605 commit 021a47e

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

0455-assign-cookies/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<p>Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.</p>
2+
3+
<p>Each child <code>i</code> has a greed factor <code>g[i]</code>, which is the minimum size of a cookie that the child will be content with; and each cookie <code>j</code> has a size <code>s[j]</code>. If <code>s[j] &gt;= g[i]</code>, we can assign the cookie <code>j</code> to the child <code>i</code>, and the child <code>i</code> will be content. Your goal is to maximize the number of your content children and output the maximum number.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> g = [1,2,3], s = [1,1]
10+
<strong>Output:</strong> 1
11+
<strong>Explanation:</strong> You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
12+
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
13+
You need to output 1.
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> g = [1,2], s = [1,2,3]
20+
<strong>Output:</strong> 2
21+
<strong>Explanation:</strong> You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
22+
You have 3 cookies and their sizes are big enough to gratify all of the children,
23+
You need to output 2.
24+
</pre>
25+
26+
<p>&nbsp;</p>
27+
<p><strong>Constraints:</strong></p>
28+
29+
<ul>
30+
<li><code>1 &lt;= g.length &lt;= 3 * 10<sup>4</sup></code></li>
31+
<li><code>0 &lt;= s.length &lt;= 3 * 10<sup>4</sup></code></li>
32+
<li><code>1 &lt;= g[i], s[j] &lt;= 2<sup>31</sup> - 1</code></li>
33+
</ul>

0455-assign-cookies/solution.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Approach 1: Greedy, Two Pointer
2+
3+
# Time: O(n log n + m log m), n = size of array `g`, m = size of array `s`
4+
# Space: O(m + n)
5+
6+
class Solution:
7+
def findContentChildren(self, g: List[int], s: List[int]) -> int:
8+
g.sort()
9+
s.sort()
10+
11+
content_children = 0
12+
cookie_idx = 0
13+
14+
while cookie_idx < len(s) and content_children < len(g):
15+
if s[cookie_idx] >= g[content_children]:
16+
content_children += 1
17+
cookie_idx += 1
18+
19+
return content_children
20+

0 commit comments

Comments
 (0)