Skip to content

Commit 0a916ca

Browse files
committed
LeetCode 1207. Unique Number of Occurrences
1 parent 47756ac commit 0a916ca

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
305305
| [1074. Number of Submatrices That Sum to Target][lc1074] | 🔴 Hard | [![python](res/py.png)][lc1074py] |
306306
| [1143. Longest Common Subsequence][lc1143] | 🟠 Medium | [![python](res/py.png)][lc1143py] |
307307
| [1155. Number of Dice Rolls With Target Sum][lc1155] | 🟠 Medium | [![python](res/py.png)][lc1155py] |
308+
| [1207. Unique Number of Occurrences][lc1207] | 🟢 Easy | [![python](res/py.png)][lc1207py] |
308309
| [1220. Count Vowels Permutation][lc1220] | 🔴 Hard | [![python](res/py.png)][lc1220py] |
309310
| [1235. Maximum Profit in Job Scheduling][lc1235] | 🔴 Hard | [![python](res/py.png)][lc1235py] |
310311
| [1239. Maximum Length of a Concatenated String with Unique Characters][lc1239] | 🟠 Medium | [![python](res/py.png)][lc1239py] |
@@ -937,6 +938,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
937938
[lc1143py]: leetcode/longest-common-subsequence.py
938939
[lc1155]: https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/
939940
[lc1155py]: ../number-of-dice-rolls-with-target-sum.py
941+
[lc1207]: https://leetcode.com/problems/unique-number-of-occurrences/
942+
[lc1207py]: leetcode/unique-number-of-occurrences.py
940943
[lc1220]: https://leetcode.com/problems/count-vowels-permutation/
941944
[lc1220py]: leetcode/count-vowels-permutation.py
942945
[lc1235]: https://leetcode.com/problems/maximum-profit-in-job-scheduling/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 1207. Unique Number of Occurrences
2+
# 🟢 Easy
3+
#
4+
# https://leetcode.com/problems/unique-number-of-occurrences/
5+
#
6+
# Tags: Array
7+
8+
import timeit
9+
from collections import Counter
10+
from typing import List
11+
12+
13+
# A fast solution is to use Collections.Counter to get the count of
14+
# occurrences of each value, then check if the count of unique values
15+
# equals the total number of values, for example using a set.
16+
#
17+
# Time complexity: O(n)
18+
# Space complexity: O(m) - Where m is the number of unique values in the
19+
# input.
20+
#
21+
# Runtime: 39 ms, faster than 91.14%
22+
# Memory Usage: 14 MB, less than 72.74%
23+
class Solution:
24+
def uniqueOccurrences(self, arr: List[int]) -> bool:
25+
c = Counter(arr)
26+
return len(c) == len(set(c.values()))
27+
28+
29+
def test():
30+
executors = [
31+
Solution,
32+
]
33+
tests = [
34+
[[1, 2, 2, 1, 1, 3], True],
35+
[[1, 2], False],
36+
[[-3, 0, 1, -3, 1, 1, 1, -3, 10, 0], True],
37+
]
38+
for executor in executors:
39+
start = timeit.default_timer()
40+
for _ in range(1):
41+
for col, t in enumerate(tests):
42+
sol = executor()
43+
result = sol.uniqueOccurrences(t[0])
44+
exp = t[1]
45+
assert result == exp, (
46+
f"\033[93m» {result} <> {exp}\033[91m for"
47+
+ f" test {col} using \033[1m{executor.__name__}"
48+
)
49+
stop = timeit.default_timer()
50+
used = str(round(stop - start, 5))
51+
cols = "{0:20}{1:10}{2:10}"
52+
res = cols.format(executor.__name__, used, "seconds")
53+
print(f"\033[92m» {res}\033[0m")
54+
55+
56+
test()

0 commit comments

Comments
 (0)