🧠
Greedy with Early Exit Optimization
💡 This approach is a minor optimization of the two-pointer method by exiting early when all children are assigned, improving runtime slightly.
Intuition
Once all children are assigned cookies, no need to continue checking remaining cookies.
Algorithm
- Sort greed and cookie arrays.
- Initialize two pointers and count.
- While pointers are valid and count less than number of children, assign cookies greedily.
- Return count.
💡 This approach avoids unnecessary iterations once all children are satisfied.
def findContentChildren(g, s):
g.sort()
s.sort()
i = j = count = 0
while i < len(g) and j < len(s) and count < len(g):
if s[j] >= g[i]:
count += 1
i += 1
j += 1
else:
j += 1
return count
# Driver code
if __name__ == '__main__':
g = [1,2,3]
s = [1,1]
print(findContentChildren(g, s)) # Output: 1
Line Notes
while i < len(g) and j < len(s) and count < len(g)Stop early if all children are assigned.
if s[j] >= g[i]Assign cookie if it satisfies the child's greed.
count += 1Increment count when a child is assigned a cookie.
i += 1; j += 1Move both pointers forward after assignment.
import java.util.*;
public class AssignCookies {
public static int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int i = 0, j = 0, count = 0;
while (i < g.length && j < s.length && count < g.length) {
if (s[j] >= g[i]) {
count++;
i++;
j++;
} else {
j++;
}
}
return count;
}
public static void main(String[] args) {
int[] g = {1,2,3};
int[] s = {1,1};
System.out.println(findContentChildren(g, s)); // Output: 1
}
}
Line Notes
while (i < g.length && j < s.length && count < g.length)Stop early when all children are assigned.
if (s[j] >= g[i])Assign cookie if it satisfies the child's greed.
count++Increment count for each successful assignment.
i++; j++;Move pointers forward after assignment.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int i = 0, j = 0, count = 0;
while (i < g.size() && j < s.size() && count < g.size()) {
if (s[j] >= g[i]) {
count++;
i++;
j++;
} else {
j++;
}
}
return count;
}
int main() {
vector<int> g = {1,2,3};
vector<int> s = {1,1};
cout << findContentChildren(g, s) << endl; // Output: 1
return 0;
}
Line Notes
while (i < g.size() && j < s.size() && count < g.size())Stop early if all children are assigned.
if (s[j] >= g[i])Assign cookie if it satisfies the child's greed.
count++Increment count for each assignment.
i++; j++;Advance pointers after assignment.
function findContentChildren(g, s) {
g.sort((a,b) => a - b);
s.sort((a,b) => a - b);
let i = 0, j = 0, count = 0;
while (i < g.length && j < s.length && count < g.length) {
if (s[j] >= g[i]) {
count++;
i++;
j++;
} else {
j++;
}
}
return count;
}
// Test
console.log(findContentChildren([1,2,3], [1,1])); // Output: 1
Line Notes
while (i < g.length && j < s.length && count < g.length)Stop early when all children are assigned.
if (s[j] >= g[i])Assign cookie if it satisfies the child's greed.
count++Increment count for each successful assignment.
i++; j++;Move pointers forward after assignment.
TimeO(n log n + m log m) due to sorting, with a slightly faster linear assignment
SpaceO(1) or O(n + m) depending on sorting implementation
Early exit can save some iterations but does not change asymptotic complexity.
💡 This optimization is useful in practice but not required to pass interviews.
Interview Verdict: Accepted
This is a minor optimization of the standard greedy approach.