diff options
author | Jack Sangdahl <[email protected]> | 2024-08-25 01:21:29 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-08-25 01:21:29 -0600 |
commit | e7204b46366f8189e0f301d7a1300f981d0eccf0 (patch) | |
tree | b2aab780d3a516f8bab97ff1791200a3d2fa91fd | |
parent | c0d5715e509aead94692d832bb5334759d65e63e (diff) | |
download | leetcode-e7204b46366f8189e0f301d7a1300f981d0eccf0.tar.gz leetcode-e7204b46366f8189e0f301d7a1300f981d0eccf0.zip |
2860. happy students
-rw-r--r-- | happy_students.c | 34 | ||||
-rw-r--r-- | happy_students.py | 33 |
2 files changed, 67 insertions, 0 deletions
diff --git a/happy_students.c b/happy_students.c new file mode 100644 index 0000000..52ea074 --- /dev/null +++ b/happy_students.c @@ -0,0 +1,34 @@ +// 2860. Happy Students +#include "leetcode.h" + +/* + * 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 i'th 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]', or 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 os that everyone remains happy + */ + +static int cmp(const int *const a, const int *const b) { return *a - *b; } + +int countWays(int *nums, int numsSize) { + qsort(nums, numsSize, sizeof(int), &cmp); + int cnt = 0; + if (nums[0]) + cnt++; + for (int i = 1; i < numsSize; i++) + if (nums[i - 1] < i && nums[i] > i) + cnt++; + if (nums[numsSize - 1] < numsSize) + cnt++; + return cnt; +} + +int main() { + int n1[] = {1, 1}, n2[] = {6, 0, 3, 3, 6, 7, 2, 7}; + printf("%d\n", countWays(n1, ARRAY_SIZE(n1))); // expect: 2 + printf("%d\n", countWays(n2, ARRAY_SIZE(n2))); // expect: 3 +} diff --git a/happy_students.py b/happy_students.py new file mode 100644 index 0000000..e94dc0d --- /dev/null +++ b/happy_students.py @@ -0,0 +1,33 @@ +# 2860. Happy Students + +""" +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 i'th 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]', or 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 os that everyone remains happy +""" + + +class Solution(object): + def countWays(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + nums.sort() + ans = int(nums[0] != 0) + nums.append(float("inf")) + for i in range(len(nums) - 1): + if nums[i] < i + 1 < nums[i + 1]: + ans += 1 + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.countWays(nums=[1, 1])) + print(obj.countWays(nums=[6, 0, 3, 3, 6, 7, 2, 7])) |