104. Assign Cookies

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.

Example 1:

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.

Example 2:

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.

Now Your Turn!

Pick the correct output for the given input

Input : student = [4, 5, 1] , cookie = [6, 4, 2]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= student.length <= 3*104
  • 0 <= cookie.length <= 3*104
  • 1 <= student[i] , cookie[j] <= 231 - 1

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution{
public:
int findMaximumCookieStudents(vector<int>& Student, vector<int>& Cookie){
//your code goes here
}
};
Test Case

Input:

Cookie
Student
Cookie
Ookie
Student
Tudent