-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2860-happy-students.js
More file actions
38 lines (34 loc) · 1.06 KB
/
2860-happy-students.js
File metadata and controls
38 lines (34 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 2860. Happy Students
* https://leetcode.com/problems/happy-students/
* Difficulty: Medium
*
* You are given a 0-indexed integer array nums of length n where n is the total number of
* students in the class. The class teacher tries to select a group of students so that
* all the students remain happy.
*
* The ith student will become happy if one of these two conditions is met:
* - The student is selected and the total number of selected students is strictly greater
* than nums[i].
* - The student is not selected and the total number of selected students is strictly less
* than nums[i].
*
* Return the number of ways to select a group of students so that everyone remains happy.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var countWays = function(nums) {
nums.sort((a, b) => a - b);
let result = 0;
let selected = 0;
if (0 < nums[0]) result++;
for (let i = 0; i < nums.length; i++) {
selected++;
if (selected > nums[i] && (i + 1 === nums.length || selected < nums[i + 1])) {
result++;
}
}
return result;
};