#455
Easy Algorithms Assign cookies
Array Two Pointers Greedy Sorting
54.7% acceptance
Jan 13, 2026
4950
454
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.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn find_content_children(mut g: Vec<i32>, mut s: Vec<i32>) -> i32 {
g.sort();
s.sort();
let mut i = 0;
let mut j = 0;
while i < g.len() && j < s.len() {
if s[j] >= g[i] {
i += 1;
}
j += 1;
}
i as i32
}
}