Skip to content
/ leetcode Public
  • Sponsor doocs/leetcode

  • Notifications You must be signed in to change notification settings
  • Fork 9k

Commit d568735

Browse files
committedDec 4, 2021
feat: add solutions to lc/lcof2 problem
lc No.0347 & lcof2 No.060.Top K Frequent Elements
1 parent 3a39203 commit d568735

File tree

7 files changed

+228
-32
lines changed

7 files changed

+228
-32
lines changed
 

‎lcof2/剑指 Offer II 060. 出现频率最高的 k 个数字/README.md

+56-6
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,14 @@
5656
class Solution:
5757
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
5858
counter = Counter(nums)
59-
6059
hp = []
6160
for num, freq in counter.items():
6261
if len(hp) == k:
63-
if freq > hp[0][0]:
64-
heapq.heappop(hp)
65-
heapq.heappush(hp, (freq, num))
62+
heapq.heappush(hp, (freq, num))
63+
heapq.heappop(hp)
6664
else:
6765
heapq.heappush(hp, (freq, num))
68-
69-
return list(map(lambda t: t[1], hp))
66+
return [t[1] for t in hp]
7067
```
7168

7269
### **Java**
@@ -97,6 +94,59 @@ class Solution {
9794
}
9895
```
9996

97+
```java
98+
class Solution {
99+
public int[] topKFrequent(int[] nums, int k) {
100+
Map<Integer, Integer> counter = new HashMap<>();
101+
for (int num : nums) {
102+
counter.put(num, counter.getOrDefault(num, 0) + 1);
103+
}
104+
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
105+
counter.forEach((num, freq) -> {
106+
if (pq.size() == k) {
107+
pq.offer(new int[]{num, freq});
108+
pq.poll();
109+
} else {
110+
pq.offer(new int[]{num, freq});
111+
}
112+
});
113+
return pq.stream().mapToInt(e -> e[0]).toArray();
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
static bool cmp(pair<int, int>& m, pair<int, int>& n) {
124+
return m.second > n.second;
125+
}
126+
vector<int> topKFrequent(vector<int>& nums, int k) {
127+
unordered_map<int, int> counter;
128+
for (auto& e : nums) ++counter[e];
129+
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&cmp)> pq(cmp);
130+
for (auto& [num, freq] : counter)
131+
{
132+
if (pq.size() == k)
133+
{
134+
pq.emplace(num, freq);
135+
pq.pop();
136+
}
137+
else pq.emplace(num, freq);
138+
}
139+
vector<int> ans;
140+
while (!pq.empty())
141+
{
142+
ans.push_back(pq.top().first);
143+
pq.pop();
144+
}
145+
return ans;
146+
}
147+
};
148+
```
149+
100150
### **...**
101151
102152
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
static bool cmp(pair<int, int>& m, pair<int, int>& n) {
4+
return m.second > n.second;
5+
}
6+
vector<int> topKFrequent(vector<int>& nums, int k) {
7+
unordered_map<int, int> counter;
8+
for (auto& e : nums) ++counter[e];
9+
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&cmp)> pq(cmp);
10+
for (auto& [num, freq] : counter)
11+
{
12+
if (pq.size() == k)
13+
{
14+
pq.emplace(num, freq);
15+
pq.pop();
16+
}
17+
else pq.emplace(num, freq);
18+
}
19+
vector<int> ans;
20+
while (!pq.empty())
21+
{
22+
ans.push_back(pq.top().first);
23+
pq.pop();
24+
}
25+
return ans;
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
class Solution:
22
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
33
counter = Counter(nums)
4-
54
hp = []
65
for num, freq in counter.items():
76
if len(hp) == k:
8-
if freq > hp[0][0]:
9-
heapq.heappop(hp)
10-
heapq.heappush(hp, (freq, num))
7+
heapq.heappush(hp, (freq, num))
8+
heapq.heappop(hp)
119
else:
1210
heapq.heappush(hp, (freq, num))
13-
14-
return list(map(lambda t: t[1], hp))
11+
return [t[1] for t in hp]

‎solution/0300-0399/0347.Top K Frequent Elements/README.md

+56-7
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
<p><strong>进阶:</strong>你所设计算法的时间复杂度 <strong>必须</strong> 优于 <code>O(n log n)</code> ,其中 <code>n</code><em> </em>是数组大小。</p>
3939

40-
4140
## 解法
4241

4342
<!-- 这里可写通用的实现逻辑 -->
@@ -54,17 +53,14 @@
5453
class Solution:
5554
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
5655
counter = Counter(nums)
57-
5856
hp = []
5957
for num, freq in counter.items():
6058
if len(hp) == k:
61-
if freq > hp[0][0]:
62-
heapq.heappop(hp)
63-
heapq.heappush(hp, (freq, num))
59+
heapq.heappush(hp, (freq, num))
60+
heapq.heappop(hp)
6461
else:
6562
heapq.heappush(hp, (freq, num))
66-
67-
return list(map(lambda t: t[1], hp))
63+
return [t[1] for t in hp]
6864
```
6965

7066
### **Java**
@@ -95,6 +91,59 @@ class Solution {
9591
}
9692
```
9793

94+
```java
95+
class Solution {
96+
public int[] topKFrequent(int[] nums, int k) {
97+
Map<Integer, Integer> counter = new HashMap<>();
98+
for (int num : nums) {
99+
counter.put(num, counter.getOrDefault(num, 0) + 1);
100+
}
101+
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
102+
counter.forEach((num, freq) -> {
103+
if (pq.size() == k) {
104+
pq.offer(new int[]{num, freq});
105+
pq.poll();
106+
} else {
107+
pq.offer(new int[]{num, freq});
108+
}
109+
});
110+
return pq.stream().mapToInt(e -> e[0]).toArray();
111+
}
112+
}
113+
```
114+
115+
### **C++**
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
static bool cmp(pair<int, int>& m, pair<int, int>& n) {
121+
return m.second > n.second;
122+
}
123+
vector<int> topKFrequent(vector<int>& nums, int k) {
124+
unordered_map<int, int> counter;
125+
for (auto& e : nums) ++counter[e];
126+
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&cmp)> pq(cmp);
127+
for (auto& [num, freq] : counter)
128+
{
129+
if (pq.size() == k)
130+
{
131+
pq.emplace(num, freq);
132+
pq.pop();
133+
}
134+
else pq.emplace(num, freq);
135+
}
136+
vector<int> ans;
137+
while (!pq.empty())
138+
{
139+
ans.push_back(pq.top().first);
140+
pq.pop();
141+
}
142+
return ans;
143+
}
144+
};
145+
```
146+
98147
### **...**
99148
100149
```

‎solution/0300-0399/0347.Top K Frequent Elements/README_EN.md

+56-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
<p>&nbsp;</p>
2727
<p><strong>Follow up:</strong> Your algorithm&#39;s time complexity must be better than <code>O(n log n)</code>, where n is the array&#39;s size.</p>
2828

29-
3029
## Solutions
3130

3231
<!-- tabs:start -->
@@ -37,17 +36,14 @@
3736
class Solution:
3837
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3938
counter = Counter(nums)
40-
4139
hp = []
4240
for num, freq in counter.items():
4341
if len(hp) == k:
44-
if freq > hp[0][0]:
45-
heapq.heappop(hp)
46-
heapq.heappush(hp, (freq, num))
42+
heapq.heappush(hp, (freq, num))
43+
heapq.heappop(hp)
4744
else:
4845
heapq.heappush(hp, (freq, num))
49-
50-
return list(map(lambda t: t[1], hp))
46+
return [t[1] for t in hp]
5147
```
5248

5349
### **Java**
@@ -76,6 +72,59 @@ class Solution {
7672
}
7773
```
7874

75+
```java
76+
class Solution {
77+
public int[] topKFrequent(int[] nums, int k) {
78+
Map<Integer, Integer> counter = new HashMap<>();
79+
for (int num : nums) {
80+
counter.put(num, counter.getOrDefault(num, 0) + 1);
81+
}
82+
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
83+
counter.forEach((num, freq) -> {
84+
if (pq.size() == k) {
85+
pq.offer(new int[]{num, freq});
86+
pq.poll();
87+
} else {
88+
pq.offer(new int[]{num, freq});
89+
}
90+
});
91+
return pq.stream().mapToInt(e -> e[0]).toArray();
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
static bool cmp(pair<int, int>& m, pair<int, int>& n) {
102+
return m.second > n.second;
103+
}
104+
vector<int> topKFrequent(vector<int>& nums, int k) {
105+
unordered_map<int, int> counter;
106+
for (auto& e : nums) ++counter[e];
107+
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&cmp)> pq(cmp);
108+
for (auto& [num, freq] : counter)
109+
{
110+
if (pq.size() == k)
111+
{
112+
pq.emplace(num, freq);
113+
pq.pop();
114+
}
115+
else pq.emplace(num, freq);
116+
}
117+
vector<int> ans;
118+
while (!pq.empty())
119+
{
120+
ans.push_back(pq.top().first);
121+
pq.pop();
122+
}
123+
return ans;
124+
}
125+
};
126+
```
127+
79128
### **...**
80129
81130
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
static bool cmp(pair<int, int>& m, pair<int, int>& n) {
4+
return m.second > n.second;
5+
}
6+
vector<int> topKFrequent(vector<int>& nums, int k) {
7+
unordered_map<int, int> counter;
8+
for (auto& e : nums) ++counter[e];
9+
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&cmp)> pq(cmp);
10+
for (auto& [num, freq] : counter)
11+
{
12+
if (pq.size() == k)
13+
{
14+
pq.emplace(num, freq);
15+
pq.pop();
16+
}
17+
else pq.emplace(num, freq);
18+
}
19+
vector<int> ans;
20+
while (!pq.empty())
21+
{
22+
ans.push_back(pq.top().first);
23+
pq.pop();
24+
}
25+
return ans;
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
class Solution:
22
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
33
counter = Counter(nums)
4-
54
hp = []
65
for num, freq in counter.items():
76
if len(hp) == k:
8-
if freq > hp[0][0]:
9-
heapq.heappop(hp)
10-
heapq.heappush(hp, (freq, num))
7+
heapq.heappush(hp, (freq, num))
8+
heapq.heappop(hp)
119
else:
1210
heapq.heappush(hp, (freq, num))
13-
14-
return list(map(lambda t: t[1], hp))
11+
return [t[1] for t in hp]

0 commit comments

Comments
 (0)
Please sign in to comment.