forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
happy-students.py
34 lines (31 loc) · 867 Bytes
/
happy-students.py
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
# Time: O(n)
# Space: O(n)
# codeforce, https://codeforces.com/contest/1782/problem/B
# freq table
class Solution(object):
def countWays(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
cnt = [0]*(len(nums)+1)
for x in nums:
cnt[x] += 1
result = prefix = 0
for i in xrange(len(nums)+1):
if prefix == i and cnt[i] == 0:
result += 1
prefix += cnt[i]
return result
# Time: O(nlogn)
# Space: O(1)
# codeforce, https://codeforces.com/contest/1782/problem/B
# sort, greedy
class Solution2(object):
def countWays(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
return sum((i == 0 or nums[i-1] < i) and (i == len(nums) or nums[i] > i) for i in xrange(len(nums)+1))