Consider a scenario where a teacher wants to distribute cookies to students, with each student receiving at most one cookie.
Given two arrays, Student and Cookie, the ith value in the Student array describes the minimum size of cookie that the ith student can be assigned. The jth value in the Cookie array represents the size of the jth cookie. If Cookie[j] >= Student[i], the jth cookie can be assigned to the ith student. Maximize the number of students assigned with cookies and output the maximum number.
Input : Student = [1, 2, 3] , Cookie = [1, 1]
Output :1
Explanation : You have 3 students and 2 cookies.
The minimum size of cookies required for students are 1 , 2 ,3.
You have 2 cookies both of size 1, So you can assign the cookie only to student having minimum cookie size 1.
So your answer is 1.
Input : Student = [1, 2] , Cookie = [1, 2, 3]
Output : 2
Explanation : You have 2 students and 3 cookies.
The minimum size of cookies required for students are 1 , 2.
You have 3 cookies and their sizes are big enough to assign cookies to all students.
So your answer is 2.
Input : Student = [4, 5, 1] , Cookie = [6, 4, 2]
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to find the maximum number of content children
int findMaximumCookieStudents(vector<int>& Student, vector<int>& Cookie) {
int n = Student.size();
int m = Cookie.size();
// Pointers
int l = 0, r = 0;
// Sorting of vectors
sort(Student.begin(), Student.end());
sort(Cookie.begin(), Cookie.end());
// Traverse through both arrays
while (l < n && r < m) {
/*If the current cookie can satisfy
the current student, move to the
next student*/
if (Cookie[r] >= Student[l]) {
l++;
}
// Move to next cookie
r++;
}
// Return the number of students who got cookies
return l;
}
};
int main() {
// Example input
vector<int> Student = {1, 2};
vector<int> Cookie = {1, 2, 3};
// Create a Solution object
Solution solution;
// Call the findMaximumCookieStudents function
int result = solution.findMaximumCookieStudents(Student, Cookie);
// Output the result
cout << "Number of students satisfied: " << result << endl;
return 0;
}
import java.util.*;
class Solution {
public int findMaximumCookieStudents(int[] Student, int[] Cookie) {
int n = Student.length;
int m = Cookie.length;
// Pointers
int l = 0, r = 0;
// Sorting of arrays
Arrays.sort(Student);
Arrays.sort(Cookie);
// Traverse through both arrays
while (l < n && r < m) {
/*If the current cookie can satisfy
the current student, move to the
next student*/
if (Cookie[r] >= Student[l]) {
l++;
}
// Move to next cookie
r++;
}
// Return number of students
return l;
}
public static void main(String[] args) {
// Example input
int[] Student = {1, 2};
int[] Cookie = {1, 2, 3};
// Create a Solution object
Solution solution = new Solution();
// Call the findMaximumCookieStudents function
int result = solution.findMaximumCookieStudents(Student, Cookie);
// Output the result
System.out.println("Number of students satisfied: " + result);
}
}
class Solution:
def findMaximumCookieStudents(self, Student, Cookie):
n = len(Student)
m = len(Cookie)
# Pointers
l, r = 0, 0
# Sorting of lists
Student.sort()
Cookie.sort()
# Traverse through both lists
while l < n and r < m:
"""If the current cookie can satisfy
the current student, move to the
next student"""
if Cookie[r] >= Student[l]:
l += 1
# Move to next cookie
r += 1
# Return number of students
return l
# Example input
Student = [1, 2]
Cookie = [1, 2, 3]
# Create a Solution object
solution = Solution()
# Call the findMaximumCookieStudents function
result = solution.findMaximumCookieStudents(Student, Cookie)
# Output the result
print("Number of students satisfied:", result)
class Solution {
findMaximumCookieStudents(Student, Cookie) {
let n = Student.length;
let m = Cookie.length;
// Pointers
let l = 0, r = 0;
// Sorting of arrays
Student.sort((a, b) => a - b);
Cookie.sort((a, b) => a - b);
// Traverse through both arrays
while (l < n && r < m) {
/*If the current cookie can satisfy
the current student, move to the
next student*/
if (Cookie[r] >= Student[l]) {
l++;
}
// Move to next cookie
r++;
}
// Return number of students
return l;
}
}
// Example input
const Student = [1, 2];
const Cookie = [1, 2, 3];
// Create a Solution object
const solution = new Solution();
// Call the findMaximumCookieStudents function
const result = solution.findMaximumCookieStudents(Student, Cookie);
// Output the result
console.log("Number of students satisfied:", result);
Q: Why is sorting necessary?
A: Sorting ensures that the smallest available cookie is assigned to the student with the smallest requirement. This maximizes the remaining larger cookies for students with higher requirements.
Q: What if there are more cookies than students?
A: The extra cookies will remain unused since the goal is to maximize the number of students who receive cookies, not the usage of cookies.
Q: What if you need to minimize the number of unused cookies?
A: This problem focuses on maximizing satisfied students, not minimizing unused cookies. To minimize unused cookies, additional logic may be required to assign larger cookies efficiently, but this might reduce the number of satisfied students.
Q: What if some students can share a cookie?
A: Modify the logic to track shared cookies. For example, divide the cookie size among eligible students and adjust the student requirements accordingly.