Skip to content

Commit 769fee7

Browse files
committed
step2 - step3
1 parent aecc925 commit 769fee7

File tree

5 files changed

+135
-13
lines changed

5 files changed

+135
-13
lines changed

347/step1.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
/*
22
20:14
33
4+
Space: O(N)
5+
Time: O(NlogN)
6+
47
出現頻度の高い順にk個の要素を返す。
58
出現頻度をカウント、カウント部分を取り出してソートしてk個残す。
69
残したカウントと合致する要素を使ってvector<int>を作成して返す。
710
8-
Space:O(N)
9-
Time: O(NlogN)
1011
*/
1112
class Solution {
12-
public:
13+
public:
1314
vector<int> topKFrequent(vector<int>& nums, int k) {
1415
map<int, int> counter;
15-
for (int num: nums) {
16+
for (int num : nums) {
1617
if (!counter.contains(num)) {
1718
counter[num] = 0;
1819
}
1920
counter[num]++;
2021
}
2122
multiset<int> sorted_counts;
22-
for (auto pair: counter){
23+
for (auto pair : counter) {
2324
sorted_counts.insert(pair.second);
2425
}
2526
auto it = sorted_counts.begin();
@@ -28,9 +29,9 @@ class Solution {
2829
}
2930
sorted_counts.erase(sorted_counts.begin(), it);
3031

31-
vector<int>answer;
32-
for (auto pair: counter) {
33-
if(sorted_counts.contains(pair.second)) {
32+
vector<int> answer;
33+
for (auto pair : counter) {
34+
if (sorted_counts.contains(pair.second)) {
3435
answer.push_back(pair.first);
3536
}
3637
}

347/step2.cpp renamed to 347/step2_1.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution {
2-
public:
2+
public:
33
vector<int> topKFrequent(vector<int>& nums, int k) {
44
map<int, int> counter;
5-
for (auto num: nums) {
5+
for (auto num : nums) {
66
if (!counter.contains(num)) {
77
counter[num] = 0;
88
}
99
counter[num]++;
1010
}
1111
vector<int> heap;
12-
for (auto pair: counter) {
12+
for (auto pair : counter) {
1313
heap.push_back(pair.second);
1414
push_heap(heap.begin(), heap.end(), std::greater<int>());
1515
if (heap.size() > k) {
@@ -18,11 +18,11 @@ class Solution {
1818
}
1919
}
2020
set<int> top_nums;
21-
for (auto num: heap) {
21+
for (auto num : heap) {
2222
top_nums.insert(num);
2323
}
2424
vector<int> top_frequent_nums;
25-
for (auto pair: counter) {
25+
for (auto pair : counter) {
2626
if (!top_nums.contains(pair.second)) {
2727
continue;
2828
}

347/step2_2_.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
quick selectを利用する手法。
3+
*/
4+
class Solution {
5+
public:
6+
vector<int> topKFrequent(vector<int>& nums, int k) {
7+
map<int, int> frequent_counter;
8+
for (int num : nums) {
9+
if (!frequent_counter.contains(num)) {
10+
frequent_counter[num] = 0;
11+
}
12+
frequent_counter[num]++;
13+
}
14+
15+
vector<int> counts;
16+
for (auto pair : frequent_counter) {
17+
counts.push_back(pair.second);
18+
}
19+
20+
int kth_frequency_count = quick_select(counts, 0, counts.size() - 1, k);
21+
vector<int> top_k_frequency_nums;
22+
for (auto pair : frequent_counter) {
23+
if (pair.second < kth_frequency_count) {
24+
continue;
25+
}
26+
top_k_frequency_nums.push_back(pair.first);
27+
}
28+
29+
return top_k_frequency_nums;
30+
}
31+
32+
private:
33+
int partition(vector<int>& arr, int left, int right) {
34+
int pivot = right;
35+
int store_index = left;
36+
for (int i = left; i < right; i++) {
37+
if (arr[i] > arr[pivot]) {
38+
swap(arr[i], arr[store_index]);
39+
store_index++;
40+
}
41+
}
42+
swap(arr[store_index], arr[pivot]);
43+
return store_index;
44+
}
45+
46+
int quick_select(vector<int>& arr, int left, int right, int k) {
47+
int pivot = partition(arr, left, right);
48+
if (pivot == k - 1) {
49+
return arr[pivot];
50+
}
51+
if (pivot < k - 1) {
52+
return quick_select(arr, pivot + 1, right, k);
53+
}
54+
return quick_select(arr, left, pivot - 1, k);
55+
}
56+
};

347/step2_3.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
priority_queueで実装するパターンで解く
3+
*/
4+
class Solution {
5+
public:
6+
vector<int> topKFrequent(vector<int>& nums, int k) {
7+
map<int, int> frequency_counter;
8+
for (int num : nums) {
9+
if (!frequency_counter.contains(num)) {
10+
frequency_counter[num] = 0;
11+
}
12+
frequency_counter[num]++;
13+
}
14+
15+
priority_queue<pair<int, int>> frequency_queue;
16+
for (auto pair : frequency_counter) {
17+
frequency_queue.push(make_pair(pair.second, pair.first));
18+
}
19+
vector<int> top_k_frequent_nums;
20+
for (int i = 0; i < k; i++) {
21+
top_k_frequent_nums.push_back(frequency_queue.top().second);
22+
frequency_queue.pop();
23+
}
24+
25+
return top_k_frequent_nums;
26+
}
27+
};

347/step3.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
他の方のPRを見てprioority_queueの解法をベースに諸々を整えたパターンでstep3を実践
3+
*/
4+
class Solution {
5+
public:
6+
vector<int> topKFrequent(vector<int>& nums, int k) {
7+
map<int, int> num_to_count;
8+
for (int num : nums) {
9+
if (!num_to_count.contains(num)) {
10+
num_to_count[num] = 0;
11+
}
12+
num_to_count[num]++;
13+
}
14+
15+
priority_queue<NumCount> num_count;
16+
for (auto [num, count] : num_to_count) {
17+
num_count.push({num, count});
18+
}
19+
20+
vector<int> answer;
21+
for (int i = 0; i < k; i++) {
22+
answer.push_back(num_count.top().num);
23+
num_count.pop();
24+
}
25+
26+
return answer;
27+
}
28+
29+
private:
30+
struct NumCount {
31+
int num;
32+
int count;
33+
34+
bool operator< (const NumCount& other) const {
35+
return count < other.count;
36+
}
37+
};
38+
};

0 commit comments

Comments
 (0)