aboutsummaryrefslogtreecommitdiff
path: root/happy_students.py
diff options
context:
space:
mode:
Diffstat (limited to 'happy_students.py')
-rw-r--r--happy_students.py33
1 files changed, 33 insertions, 0 deletions
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]))