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, the task is 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, 2, 3], cookie[] = [1, 1]
Output: 1
Explanation: We can only assign the first or second cookie to the first child.
Input: greed[] = [1, 2], cookie[] = [1, 2, 3]
Output: 2
Explanation: We can assign the first cookie to the first child and the second cookie to the second child.
Using Greedy Algorithm
The idea is to use Greedy Algorithm to assign smallest cookie to each child such that the child gets satisfied. So, we can sort both greed[] and cookie[] array and start assigning the smallest cookies to the children with smallest greed. We can initialize two pointers, say i and j to the beginning of greed[] and cookie[] respectively. Now, we can have 2 cases:
- If greed[i] <= cookie[j], this means that we can satisfy the ith child by assigning him jth cookie. So, we increase the count of satisfied children by 1 and increment both the pointers to move to the next child and cookie.
- If greed[i] > cookie[j], this means that we cannot satisfy the ith child by assigning him jth cookie as he wants a bigger cookie. So, increment j to move to the next bigger cookie.
We continue traversing the array until either pointer reaches the end of its respective array.
C++
// C++ Code for assigning cookies using Greedy Algorithm
#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, 2, 3};
vector<int> cookie = {1, 1};
cout << maxChildren(greed, cookie);
}
C
// 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, 2, 3};
int cookie[] = {1, 1};
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;
}
Java
// Java Code for assigning cookies using Greedy Algorithm
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, 2, 3};
int[] cookie = {1, 1};
System.out.println(maxChildren(greed, cookie));
}
}
Python
# Python Code for assigning cookies using Greedy Algorithm
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, 2, 3]
cookie = [1, 1]
print(maxChildren(greed, cookie))
C#
// C# Code for assigning cookies using Greedy Algorithm
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, 2, 3 };
int[] cookie = { 1, 1 };
Console.WriteLine(MaxChildren(greed, cookie));
}
}
JavaScript
// JavaScript Code for assigning cookies using Greedy Algorithm
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, 2, 3];
const cookie = [1, 1];
console.log(maxChildren(greed, cookie));
Time Complexity: O(nlogn + mlogm), where n and m are the sizes of greed[] and cookie[] respectively. This is because we are sorting both the arrays before traversing over them.
Auxiliary Space: O(1)
Similar Reads
Assign Campus Bikes to Workers On a campus represented on the X-Y, there are n workers and m bikes (n <= m). You are given: workers: an array of length n, where workers[i] = [xi, yi] is the position of the ith workerbikes: an array of length m, where bikes[j] = [xj, yj] is the position of the jth bikeAssign a bike to each work
8 min read
Passing the Assignment Rachel is submitting assignment from every student. She has given them a few minutes to arrange their assignments before submission. Just then Rahul remembers that his assignment is with Rohan. Rohan has to pass the assignment to Rahul. All the students are sitting in a straight line. He cannot pass
11 min read
Channel Assignment Problem There are M transmitter and N receiver stations. Given a matrix that keeps track of the number of packets to be transmitted from a given transmitter to a receiver. If the (i; j)-th entry of the matrix is k, it means at that time the station i has k packets for transmission to station j. During a tim
12 min read
Worker-Bike Assignments (Campus Bikes) Given an array workers[] and bikes[], which represents position of workers and bikes on a 2D plane, the task is to assign each worker to a bike based on the shortest Manhattan distance between them. If multiple pairs share the same distance then prioritize by worker's index and then by bike index. P
7 min read
Program for assigning usernames using Trie Suppose there is a queue of n users and your task is to assign a username to them. The system works in the following way. Every user has preferred login in the form of a string 's' s consists only of small case letters and numbers. User name is assigned in the following order s, s0, s1, s2....s11...
15+ min read
Count Ways To Assign Unique Cap To Every Person Given n people and 100 types of caps labelled from 1 to 100, along with a 2D integer array caps where caps[i] represents the list of caps preferred by the i-th person, the task is to determine the number of ways the n people can wear different caps.Example:Input: caps = [[3, 4], [4, 5], [5]] Output:
15+ min read