Skip to content

Commit c62e79e

Browse files
committed
feat: add solutions to lc problem: No.1207
No.1207.Unique Number of Occurrences
1 parent d7150f6 commit c62e79e

File tree

8 files changed

+75
-104
lines changed

8 files changed

+75
-104
lines changed

solution/1200-1299/1207.Unique Number of Occurrences/README.md

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46-
“哈希表 - 计数器”实现。
46+
**方法一:哈希表**
47+
48+
我们用哈希表 `cnt` 统计数组 `arr` 中每个数的出现次数,然后用哈希表 `vis` 统计出现次数的种类,最后判断 `cnt``vis` 的大小是否相等即可。
49+
50+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `arr` 的长度。
4751

4852
<!-- tabs:start -->
4953

@@ -54,13 +58,8 @@
5458
```python
5559
class Solution:
5660
def uniqueOccurrences(self, arr: List[int]) -> bool:
57-
counter = Counter(arr)
58-
s = set()
59-
for num in counter.values():
60-
if num in s:
61-
return False
62-
s.add(num)
63-
return True
61+
cnt = Counter(arr)
62+
return len(set(cnt.values())) == len(cnt)
6463
```
6564

6665
### **Java**
@@ -70,18 +69,11 @@ class Solution:
7069
```java
7170
class Solution {
7271
public boolean uniqueOccurrences(int[] arr) {
73-
Map<Integer, Integer> counter = new HashMap<>();
74-
for (int e : arr) {
75-
counter.put(e, counter.getOrDefault(e, 0) + 1);
76-
}
77-
Set<Integer> s = new HashSet<>();
78-
for (int num : counter.values()) {
79-
if (s.contains(num)) {
80-
return false;
81-
}
82-
s.add(num);
72+
Map<Integer, Integer> cnt = new HashMap<>();
73+
for (int x : arr) {
74+
cnt.merge(x, 1, Integer::sum);
8375
}
84-
return true;
76+
return new HashSet<>(cnt.values()).size() == cnt.size();
8577
}
8678
}
8779
```
@@ -92,15 +84,16 @@ class Solution {
9284
class Solution {
9385
public:
9486
bool uniqueOccurrences(vector<int>& arr) {
95-
unordered_map<int, int> counter;
96-
for (auto e : arr) {
97-
++counter[e];
87+
unordered_map<int, int> cnt;
88+
for (int& x : arr) {
89+
++cnt[x];
9890
}
99-
unordered_set<int> s;
100-
for (auto e : counter) {
101-
int num = e.second;
102-
if (s.count(num)) return false;
103-
s.insert(num);
91+
unordered_set<int> vis;
92+
for (auto& [_, v] : cnt) {
93+
if (vis.count(v)) {
94+
return false;
95+
}
96+
vis.insert(v);
10497
}
10598
return true;
10699
}
@@ -111,16 +104,16 @@ public:
111104
112105
```go
113106
func uniqueOccurrences(arr []int) bool {
114-
counter := make(map[int]int)
115-
for _, e := range arr {
116-
counter[e]++
107+
cnt := map[int]int{}
108+
for _, x := range arr {
109+
cnt[x]++
117110
}
118-
s := make(map[int]bool)
119-
for _, num := range counter {
120-
if s[num] {
111+
vis := map[int]bool{}
112+
for _, v := range cnt {
113+
if vis[v] {
121114
return false
122115
}
123-
s[num] = true
116+
vis[v] = true
124117
}
125118
return true
126119
}

solution/1200-1299/1207.Unique Number of Occurrences/README_EN.md

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,20 @@
4545
```python
4646
class Solution:
4747
def uniqueOccurrences(self, arr: List[int]) -> bool:
48-
counter = Counter(arr)
49-
s = set()
50-
for num in counter.values():
51-
if num in s:
52-
return False
53-
s.add(num)
54-
return True
48+
cnt = Counter(arr)
49+
return len(set(cnt.values())) == len(cnt)
5550
```
5651

5752
### **Java**
5853

5954
```java
6055
class Solution {
6156
public boolean uniqueOccurrences(int[] arr) {
62-
Map<Integer, Integer> counter = new HashMap<>();
63-
for (int e : arr) {
64-
counter.put(e, counter.getOrDefault(e, 0) + 1);
57+
Map<Integer, Integer> cnt = new HashMap<>();
58+
for (int x : arr) {
59+
cnt.merge(x, 1, Integer::sum);
6560
}
66-
Set<Integer> s = new HashSet<>();
67-
for (int num : counter.values()) {
68-
if (s.contains(num)) {
69-
return false;
70-
}
71-
s.add(num);
72-
}
73-
return true;
61+
return new HashSet<>(cnt.values()).size() == cnt.size();
7462
}
7563
}
7664
```
@@ -81,15 +69,16 @@ class Solution {
8169
class Solution {
8270
public:
8371
bool uniqueOccurrences(vector<int>& arr) {
84-
unordered_map<int, int> counter;
85-
for (auto e : arr) {
86-
++counter[e];
72+
unordered_map<int, int> cnt;
73+
for (int& x : arr) {
74+
++cnt[x];
8775
}
88-
unordered_set<int> s;
89-
for (auto e : counter) {
90-
int num = e.second;
91-
if (s.count(num)) return false;
92-
s.insert(num);
76+
unordered_set<int> vis;
77+
for (auto& [_, v] : cnt) {
78+
if (vis.count(v)) {
79+
return false;
80+
}
81+
vis.insert(v);
9382
}
9483
return true;
9584
}
@@ -100,16 +89,16 @@ public:
10089
10190
```go
10291
func uniqueOccurrences(arr []int) bool {
103-
counter := make(map[int]int)
104-
for _, e := range arr {
105-
counter[e]++
92+
cnt := map[int]int{}
93+
for _, x := range arr {
94+
cnt[x]++
10695
}
107-
s := make(map[int]bool)
108-
for _, num := range counter {
109-
if s[num] {
96+
vis := map[int]bool{}
97+
for _, v := range cnt {
98+
if vis[v] {
11099
return false
111100
}
112-
s[num] = true
101+
vis[v] = true
113102
}
114103
return true
115104
}

solution/1200-1299/1207.Unique Number of Occurrences/Solution.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
class Solution {
22
public:
33
bool uniqueOccurrences(vector<int>& arr) {
4-
unordered_map<int, int> counter;
5-
for (auto e : arr) {
6-
++counter[e];
4+
unordered_map<int, int> cnt;
5+
for (int& x : arr) {
6+
++cnt[x];
77
}
8-
unordered_set<int> s;
9-
for (auto e : counter) {
10-
int num = e.second;
11-
if (s.count(num)) return false;
12-
s.insert(num);
8+
unordered_set<int> vis;
9+
for (auto& [_, v] : cnt) {
10+
if (vis.count(v)) {
11+
return false;
12+
}
13+
vis.insert(v);
1314
}
1415
return true;
1516
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
func uniqueOccurrences(arr []int) bool {
2-
counter := make(map[int]int)
3-
for _, e := range arr {
4-
counter[e]++
2+
cnt := map[int]int{}
3+
for _, x := range arr {
4+
cnt[x]++
55
}
6-
s := make(map[int]bool)
7-
for _, num := range counter {
8-
if s[num] {
6+
vis := map[int]bool{}
7+
for _, v := range cnt {
8+
if vis[v] {
99
return false
1010
}
11-
s[num] = true
11+
vis[v] = true
1212
}
1313
return true
1414
}
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
class Solution {
22
public boolean uniqueOccurrences(int[] arr) {
3-
Map<Integer, Integer> counter = new HashMap<>();
4-
for (int e : arr) {
5-
counter.put(e, counter.getOrDefault(e, 0) + 1);
3+
Map<Integer, Integer> cnt = new HashMap<>();
4+
for (int x : arr) {
5+
cnt.merge(x, 1, Integer::sum);
66
}
7-
Set<Integer> s = new HashSet<>();
8-
for (int num : counter.values()) {
9-
if (s.contains(num)) {
10-
return false;
11-
}
12-
s.add(num);
13-
}
14-
return true;
7+
return new HashSet<>(cnt.values()).size() == cnt.size();
158
}
169
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
class Solution:
22
def uniqueOccurrences(self, arr: List[int]) -> bool:
3-
counter = Counter(arr)
4-
s = set()
5-
for num in counter.values():
6-
if num in s:
7-
return False
8-
s.add(num)
9-
return True
3+
cnt = Counter(arr)
4+
return len(set(cnt.values())) == len(cnt)

solution/CONTEST_README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
1313
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
14-
| LV3 | 5% | Guardian | &ge;2249.85 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
15-
| LV2 | 20% | Knight | &ge;1883.49 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
14+
| LV3 | 5% | Guardian | &ge;2252.78 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
15+
| LV2 | 20% | Knight | &ge;1884.83 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
1616
| LV1 | 75% | - | - | - |
1717

1818
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。

solution/contest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ def generate_contest_list():
127127
128128
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
129129
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
130-
| LV3 | 5% | Guardian | &ge;2249.85 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
131-
| LV2 | 20% | Knight | &ge;1883.49 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
130+
| LV3 | 5% | Guardian | &ge;2252.78 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
131+
| LV2 | 20% | Knight | &ge;1884.83 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
132132
| LV1 | 75% | - | - | - |
133133
134134
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。

0 commit comments

Comments
 (0)