Skip to content

Commit 71a5f50

Browse files
Add some heap solutions
1 parent 8eeb4e1 commit 71a5f50

File tree

5 files changed

+127
-4
lines changed

5 files changed

+127
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,10 @@
253253

254254
|S.No|Problem|Difficulty|Solution|
255255
|-|------|--------|------|
256-
|1|[Sort Array By Increasing Frequency](https://leetcode.com/problems/sort-array-by-increasing-frequency/)|Easy|[Solution](/solutions/heaps)|
257-
|2|[Kth Largest Element in An Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)|Medium|[Solution](/solutions/heaps)|
258-
|3|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/)|Medium|[Solution](/solutions/heaps)|
259-
|4|[Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/)|Medium|[Solution](/solutions/heaps)|
256+
|1|[Sort Array By Increasing Frequency](https://leetcode.com/problems/sort-array-by-increasing-frequency/)|Easy|[Solution](/solutions/heaps/sort-array-by-increasing-frequency.md)|
257+
|2|[Kth Largest Element in An Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)|Medium|[Solution](/solutions/heaps/kth-largest-element-in-array.md)|
258+
|3|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/)|Medium|[Solution](/solutions/heaps/top-k-frequent-elements.md)|
259+
|4|[Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/)|Medium|[Solution](/solutions/heaps/sort-characters-by-frequency.md)|
260260
|5|[Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/)|Medium|[Solution](/solutions/heaps)|
261261
|6|[Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/)|Medium|[Solution](/solutions/heaps)|
262262
|7|[K Closest Points To Origin](https://leetcode.com/problems/k-closest-points-to-origin/)|Medium|[Solution](/solutions/heaps)|
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Kth Largest Element in Array
2+
3+
```cpp
4+
class Solution {
5+
public:
6+
int findKthLargest(vector<int>& nums, int k) {
7+
priority_queue<int, vector<int>, greater<>> pq;
8+
for(auto N : nums) {
9+
pq.push(N);
10+
if(pq.size() > k) {
11+
pq.pop();
12+
}
13+
}
14+
return pq.top();
15+
}
16+
};
17+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Sort Array By Increasing Frequency
2+
3+
```cpp
4+
class Solution {
5+
public:
6+
vector<int> frequencySort(vector<int>& nums) {
7+
8+
unordered_map<int, int> cnt;
9+
10+
for(int n : nums) cnt[n]++;
11+
12+
auto cmp = [&](int a, int b) {
13+
if(cnt[a] == cnt[b]) {
14+
return a < b;
15+
}
16+
return cnt[a] > cnt[b];
17+
};
18+
priority_queue<int, vector<int>, decltype(cmp)> pq(cmp);
19+
20+
for(auto &[n, c] : cnt) {
21+
pq.push(n);
22+
}
23+
24+
vector<int> ans;
25+
26+
while(pq.size()) {
27+
int c = pq.top();
28+
pq.pop();
29+
30+
while(cnt[c]--) {
31+
ans.push_back(c);
32+
}
33+
}
34+
35+
return ans;
36+
}
37+
};
38+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Sort Characters By Frequency
2+
3+
```cpp
4+
class Solution {
5+
public:
6+
string frequencySort(string s) {
7+
8+
unordered_map<char, int> cnt;
9+
10+
for(char c : s) {
11+
cnt[c]++;
12+
}
13+
14+
auto cmp = [&](char a, char b) { return cnt[a] < cnt[b]; };
15+
priority_queue<char, vector<char>, decltype(cmp)> pq(cmp);
16+
17+
for(auto &[c, n] : cnt) {
18+
pq.push(c);
19+
}
20+
21+
string ans = "";
22+
23+
while(pq.size()) {
24+
char c = pq.top();
25+
pq.pop();
26+
27+
ans += string(cnt[c], c);
28+
}
29+
30+
return ans;
31+
}
32+
};
33+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Top K Frequent Elements
2+
3+
```cpp
4+
class Solution {
5+
public:
6+
vector<int> topKFrequent(vector<int>& nums, int k) {
7+
8+
unordered_map<int, int> cnt;
9+
10+
auto cmp = [&](int a, int b) { return cnt[a] > cnt[b]; };
11+
priority_queue<int, vector<int>, decltype(cmp)> pq(cmp);
12+
13+
for(int n : nums) cnt[n]++;
14+
15+
for(auto &[n, c] : cnt) {
16+
pq.push(n);
17+
}
18+
19+
vector<int> ans;
20+
21+
while(pq.size() > k) {
22+
int c = pq.top();
23+
pq.pop();
24+
}
25+
26+
while(pq.size()) {
27+
int c = pq.top();
28+
pq.pop();
29+
ans.push_back(c);
30+
}
31+
32+
return ans;
33+
}
34+
};
35+
```

0 commit comments

Comments
 (0)