#2860
Happy Students
specialist · 915 · lc medium +31 · verified · 50.9% accepted · 182 likes · top 39%
Description
A 0-indexed integer array nums of length n represents student preferences. A teacher selects a group so all students are happy.
Student i is happy when:
- They are selected and the group size strictly exceeds nums[i], or
- They are not selected and the group size is strictly less than nums[i].
Return the number of ways to select a group (possibly empty or all students) so everyone is happy.
Example 1:
Input: nums = [1,1]
Output: 2
Explanation:
The two possible ways are:
The class teacher selects no student.
The class teacher selects both students to form the group.
If the class teacher selects just one student to form a group then the both students will not be happy. Therefore, there are only two possible ways.
Example 2:
Input: nums = [6,0,3,3,6,7,2,7]
Output: 3
Explanation:
The three possible ways are:
The class teacher selects the student with index = 1 to form the group.
The class teacher selects the students with index = 1, 2, 3, 6 to form the group.
The class teacher selects all the students to form the group.
Code
1
2
3