-
Notifications
You must be signed in to change notification settings - Fork 1
/
1207-unique_number_of_occurences.py
38 lines (31 loc) · 1.25 KB
/
1207-unique_number_of_occurences.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
35
36
37
38
"""
https://leetcode.com/problems/unique-number-of-occurrences/
Strat:
Make a count of how frequently each number occurs
Stats: O(n) time, O(n) space -- one pass thru array & one pass thru dict + construct two dicts
Runtime: 20 ms, faster than 92.90% of Python online submissions for Unique Number of Occurrences.
Memory Usage: 12.9 MB, less than 100.00% of Python online submissions for Unique Number of Occurrences.
"""
class Solution(object):
def uniqueOccurrences(self, arr):
"""
:type arr: List[int]
:rtype: bool
"""
#count how frequently each num occurs
counts = {}
for num in arr:
counts[num] = counts.get(num, 0) + 1
#count the occurence. if the occurence is already considered, we do not
#have a unique number of occurences
occurences = {}
for value in counts.values():
if occurences.get(value, 0) == 0: #seen for first time
occurences[value] = True
else:
return False
return True
# #or...
# occurences = counts.values()
# unique_occurences = set(occurences)
# return len(occurences) == len(unique_occurences)