Assign Maximum Cookies
Given two arrays, greed[] and cookie[] such that greed[i] denotes the minimum cookie size wanted by ith child and cookie[i] denotes the size of ith cookie, we have to find the maximum number of children that can be satisfied by assigning them cookies, with each child getting at most 1 cookie.
Note: A child will be satisfied if he is assigned a cookie of size at least equal to his greed. In other words, the ith child will be satified with jth cookie only if greed[i] <= cookie[j].
Examples:
Input: greed[] = [1, 10, 3], cookie[] = [1, 2,3]
Output: 2
Explanation: We can only assign cookie to the first child and third child.Input: greed[] = [10,100], cookie[] = [1, 2]
Output: 0
Explanation: We can not assign cookies to children.
[Approach] Using Sorting and Two Pointer - O(nlogn + mlogm) Time and O(1) Space
The idea is to assign smallest cookie to each child greedily such that the child gets satisfied. So, we can sort both greed[] and cookie[] and start assigning the smallest cookies to the children with smallest greed.
Steps to solve the problem:
- Sort the greed array
greed
and the cookie arrays
. - Initialize two pointers:
i
(index for children),j
(index for cookies) - Initialize
count = 0
to track the number of satisfied children. - Iterate while both pointers are within their array bounds:
- If cookie
[j] >= greed[i]
, the cookie can satisfy the child: Incrementcount
and Move both pointers.
- Else cookie is too small and Move only the cookie pointer/
- Return
count
as the total number of satisfied children.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int maxChildren(vector<int> &greed, vector<int> &cookie) {
sort(greed.begin(), greed.end());
sort(cookie.begin(), cookie.end());
// Initialize pointers to the beginning of the arrays
int i = 0, j = 0;
int cnt = 0;
// Iterate until we reach the end of either array
while(i < greed.size() && j < cookie.size()) {
// If ith child can be satisfied with jth cookie,
// assign it to him and increment the count
if(greed[i] <= cookie[j]) {
cnt++;
i++;
j++;
}
// If ithe child cannot be satified with jth
// cookie, move to the next bigger cookie
else
j++;
}
return cnt;
}
int main() {
vector<int> greed = {1, 10, 3};
vector<int> cookie = {1, 2,3};
cout << maxChildren(greed, cookie);
}
// C Code for assigning cookies using Greedy Algorithm
#include <stdio.h>
int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}
int maxChildren(int greed[], int n, int cookie[], int m) {
qsort(greed, n, sizeof(int), compare);
qsort(cookie, m, sizeof(int), compare);
// Initialize pointers to the beginning of the arrays
int i = 0, j = 0;
int cnt = 0;
// Iterate until we reach the end of either array
while (i < n && j < m) {
// If ith child can be satisfied with jth cookie,
// assign it to him and increment the count
if (greed[i] <= cookie[j]) {
cnt++;
i++;
j++;
}
// If ith child cannot be satisfied with jth
// cookie, move to the next bigger cookie
else
j++;
}
return cnt;
}
int main() {
int greed[] = {1, 10, 3};
int cookie[] = {1, 2,3};
int n = sizeof(greed)/sizeof(greed[0]);
int m = sizeof(cookie)/sizeof(cookie[0]);
int result = maxChildren(greed, n, cookie, m);
printf("%d\n", result);
return 0;
}
import java.util.Arrays;
class GfG {
static int maxChildren(int[] greed, int[] cookie) {
Arrays.sort(greed);
Arrays.sort(cookie);
// Initialize pointers to the beginning of the arrays
int i = 0, j = 0;
int cnt = 0;
// Iterate until we reach the end of either array
while (i < greed.length && j < cookie.length) {
// If ith child can be satisfied with jth cookie,
// assign it to him and increment the count
if (greed[i] <= cookie[j]) {
cnt++;
i++;
j++;
}
// If ith child cannot be satisfied with jth
// cookie, move to the next bigger cookie
else
j++;
}
return cnt;
}
public static void main(String[] args) {
int[] greed = {1, 10, 3};
int[] cookie = {1, 2,3};
System.out.println(maxChildren(greed, cookie));
}
}
def maxChildren(greed, cookie):
greed.sort()
cookie.sort()
# Initialize pointers to the beginning of the arrays
i = 0
j = 0
cnt = 0
# Iterate until we reach the end of either array
while i < len(greed) and j < len(cookie):
# If ith child can be satisfied with jth cookie,
# assign it to him and increment the count
if greed[i] <= cookie[j]:
cnt += 1
i += 1
j += 1
# If ith child cannot be satisfied with jth
# cookie, move to the next bigger cookie
else:
j += 1
return cnt
if __name__ == "__main__":
greed = [1, 10, 3]
cookie = [1, 2,3]
print(maxChildren(greed, cookie))
using System;
class GfG {
static int MaxChildren(int[] greed, int[] cookie) {
Array.Sort(greed);
Array.Sort(cookie);
// Initialize pointers to the beginning of the arrays
int i = 0, j = 0;
int cnt = 0;
// Iterate until we reach the end of either array
while (i < greed.Length && j < cookie.Length) {
// If ith child can be satisfied with jth cookie,
// assign it to him and increment the count
if (greed[i] <= cookie[j]) {
cnt++;
i++;
j++;
}
// If ith child cannot be satisfied with jth
// cookie, move to the next bigger cookie
else
j++;
}
return cnt;
}
static void Main(string[] args) {
int[] greed = { 1, 10, 3 };
int[] cookie = { 1, 2,3 };
Console.WriteLine(MaxChildren(greed, cookie));
}
}
function maxChildren(greed, cookie) {
greed.sort((a, b) => a - b);
cookie.sort((a, b) => a - b);
// Initialize pointers to the beginning of the arrays
let i = 0, j = 0;
let cnt = 0;
// Iterate until we reach the end of either array
while (i < greed.length && j < cookie.length) {
// If ith child can be satisfied with jth cookie,
// assign it to him and increment the count
if (greed[i] <= cookie[j]) {
cnt++;
i++;
j++;
}
// If ith child cannot be satisfied with jth
// cookie, move to the next bigger cookie
else
j++;
}
return cnt;
}
// Driver Code
const greed = [1, 10, 3];
const cookie = [1, 2,3];
console.log(maxChildren(greed, cookie));
Output
2