aboutsummaryrefslogtreecommitdiff
path: root/happy_students.c
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-08-25 01:21:29 -0600
committerJack Sangdahl <[email protected]>2024-08-25 01:21:29 -0600
commite7204b46366f8189e0f301d7a1300f981d0eccf0 (patch)
treeb2aab780d3a516f8bab97ff1791200a3d2fa91fd /happy_students.c
parentc0d5715e509aead94692d832bb5334759d65e63e (diff)
downloadleetcode-e7204b46366f8189e0f301d7a1300f981d0eccf0.tar.gz
leetcode-e7204b46366f8189e0f301d7a1300f981d0eccf0.zip
2860. happy students
Diffstat (limited to 'happy_students.c')
-rw-r--r--happy_students.c34
1 files changed, 34 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
+}