Skip to content

Commit 92539a3

Browse files
committed
feat: add solutions to lc problems: No.0347,0451,0973
- No.0347.Top K Frequent Elements - No.0451.Sort Characters By Frequency - No.0973.K Closest Points to Origin
1 parent 22ba898 commit 92539a3

File tree

10 files changed

+333
-0
lines changed

10 files changed

+333
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,29 @@ function topKFrequent(nums: number[], k: number): number[] {
130130
}
131131
```
132132

133+
```ts
134+
function topKFrequent(nums: number[], k: number): number[] {
135+
const map = new Map<number, number>();
136+
let maxCount = 0;
137+
for (const num of nums) {
138+
map.set(num, (map.get(num) ?? 0) + 1);
139+
maxCount = Math.max(maxCount, map.get(num));
140+
}
141+
142+
const res = [];
143+
while (k > 0) {
144+
for (const key of map.keys()) {
145+
if (map.get(key) === maxCount) {
146+
res.push(key);
147+
k--;
148+
}
149+
}
150+
maxCount--;
151+
}
152+
return res;
153+
}
154+
```
155+
133156
### **C++**
134157

135158
```cpp
@@ -162,6 +185,39 @@ public:
162185
};
163186
```
164187
188+
### **Rust**
189+
190+
```rust
191+
use std::collections::HashMap;
192+
impl Solution {
193+
pub fn top_k_frequent(nums: Vec<i32>, k: i32) -> Vec<i32> {
194+
let mut map = HashMap::new();
195+
let mut max_count = 0;
196+
for &num in nums.iter() {
197+
let val = map.get(&num).unwrap_or(&0) + 1;
198+
map.insert(num, val);
199+
max_count = max_count.max(val);
200+
}
201+
let mut k = k as usize;
202+
let mut res = vec![0; k];
203+
while k > 0 {
204+
let mut next = 0;
205+
for key in map.keys() {
206+
let val = map[key];
207+
if val == max_count {
208+
res[k - 1] = *key;
209+
k -= 1;
210+
} else if val < max_count {
211+
next = next.max(val);
212+
}
213+
}
214+
max_count = next;
215+
}
216+
res
217+
}
218+
}
219+
```
220+
165221
### **...**
166222

167223
```

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,29 @@ function topKFrequent(nums: number[], k: number): number[] {
111111
}
112112
```
113113

114+
```ts
115+
function topKFrequent(nums: number[], k: number): number[] {
116+
const map = new Map<number, number>();
117+
let maxCount = 0;
118+
for (const num of nums) {
119+
map.set(num, (map.get(num) ?? 0) + 1);
120+
maxCount = Math.max(maxCount, map.get(num));
121+
}
122+
123+
const res = [];
124+
while (k > 0) {
125+
for (const key of map.keys()) {
126+
if (map.get(key) === maxCount) {
127+
res.push(key);
128+
k--;
129+
}
130+
}
131+
maxCount--;
132+
}
133+
return res;
134+
}
135+
```
136+
114137
### **C++**
115138

116139
```cpp
@@ -143,6 +166,39 @@ public:
143166
};
144167
```
145168
169+
### **Rust**
170+
171+
```rust
172+
use std::collections::HashMap;
173+
impl Solution {
174+
pub fn top_k_frequent(nums: Vec<i32>, k: i32) -> Vec<i32> {
175+
let mut map = HashMap::new();
176+
let mut max_count = 0;
177+
for &num in nums.iter() {
178+
let val = map.get(&num).unwrap_or(&0) + 1;
179+
map.insert(num, val);
180+
max_count = max_count.max(val);
181+
}
182+
let mut k = k as usize;
183+
let mut res = vec![0; k];
184+
while k > 0 {
185+
let mut next = 0;
186+
for key in map.keys() {
187+
let val = map[key];
188+
if val == max_count {
189+
res[k - 1] = *key;
190+
k -= 1;
191+
} else if val < max_count {
192+
next = next.max(val);
193+
}
194+
}
195+
max_count = next;
196+
}
197+
res
198+
}
199+
}
200+
```
201+
146202
### **...**
147203

148204
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::collections::HashMap;
2+
impl Solution {
3+
pub fn top_k_frequent(nums: Vec<i32>, k: i32) -> Vec<i32> {
4+
let mut map = HashMap::new();
5+
let mut max_count = 0;
6+
for &num in nums.iter() {
7+
let val = map.get(&num).unwrap_or(&0) + 1;
8+
map.insert(num, val);
9+
max_count = max_count.max(val);
10+
}
11+
let mut k = k as usize;
12+
let mut res = vec![0; k];
13+
while k > 0 {
14+
let mut next = 0;
15+
for key in map.keys() {
16+
let val = map[key];
17+
if val == max_count {
18+
res[k - 1] = *key;
19+
k -= 1;
20+
} else if val < max_count {
21+
next = next.max(val);
22+
}
23+
}
24+
max_count = next;
25+
}
26+
res
27+
}
28+
}

solution/0400-0499/0451.Sort Characters By Frequency/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,40 @@ func frequencySort(s string) string {
140140
}
141141
```
142142

143+
### **TypeScript**
144+
145+
```ts
146+
function frequencySort(s: string): string {
147+
const map = new Map<string, number>();
148+
for (const c of s) {
149+
map.set(c, (map.get(c) ?? 0) + 1);
150+
}
151+
return [...map.entries()]
152+
.sort((a, b) => b[1] - a[1])
153+
.map(([k, v]) => k.padStart(v, k))
154+
.join('');
155+
}
156+
```
157+
158+
### **Rust**
159+
160+
```rust
161+
use std::collections::HashMap;
162+
impl Solution {
163+
pub fn frequency_sort(s: String) -> String {
164+
let mut map = HashMap::new();
165+
for c in s.chars() {
166+
map.insert(c, map.get(&c).unwrap_or(&0) + 1);
167+
}
168+
let mut arr = map.into_iter().collect::<Vec<(char, i32)>>();
169+
arr.sort_unstable_by(|(_, a), (_, b)| b.cmp(&a));
170+
arr.into_iter()
171+
.map(|(c, v)| vec![c; v as usize].into_iter().collect::<String>())
172+
.collect()
173+
}
174+
}
175+
```
176+
143177
### **...**
144178

145179
```

solution/0400-0499/0451.Sort Characters By Frequency/README_EN.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,40 @@ func frequencySort(s string) string {
126126
}
127127
```
128128

129+
### **TypeScript**
130+
131+
```ts
132+
function frequencySort(s: string): string {
133+
const map = new Map<string, number>();
134+
for (const c of s) {
135+
map.set(c, (map.get(c) ?? 0) + 1);
136+
}
137+
return [...map.entries()]
138+
.sort((a, b) => b[1] - a[1])
139+
.map(([k, v]) => k.padStart(v, k))
140+
.join('');
141+
}
142+
```
143+
144+
### **Rust**
145+
146+
```rust
147+
use std::collections::HashMap;
148+
impl Solution {
149+
pub fn frequency_sort(s: String) -> String {
150+
let mut map = HashMap::new();
151+
for c in s.chars() {
152+
map.insert(c, map.get(&c).unwrap_or(&0) + 1);
153+
}
154+
let mut arr = map.into_iter().collect::<Vec<(char, i32)>>();
155+
arr.sort_unstable_by(|(_, a), (_, b)| b.cmp(&a));
156+
arr.into_iter()
157+
.map(|(c, v)| vec![c; v as usize].into_iter().collect::<String>())
158+
.collect()
159+
}
160+
}
161+
```
162+
129163
### **...**
130164

131165
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::collections::HashMap;
2+
impl Solution {
3+
pub fn frequency_sort(s: String) -> String {
4+
let mut map = HashMap::new();
5+
for c in s.chars() {
6+
map.insert(c, map.get(&c).unwrap_or(&0) + 1);
7+
}
8+
let mut arr = map.into_iter().collect::<Vec<(char, i32)>>();
9+
arr.sort_unstable_by(|(_, a), (_, b)| b.cmp(&a));
10+
arr.into_iter()
11+
.map(|(c, v)| vec![c; v as usize].into_iter().collect::<String>())
12+
.collect()
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function frequencySort(s: string): string {
2+
const map = new Map<string, number>();
3+
for (const c of s) {
4+
map.set(c, (map.get(c) ?? 0) + 1);
5+
}
6+
return [...map.entries()]
7+
.sort((a, b) => b[1] - a[1])
8+
.map(([k, v]) => k.padStart(v, k))
9+
.join('');
10+
}

solution/0900-0999/0973.K Closest Points to Origin/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,55 @@
6464
<!-- 这里可写当前语言的特殊实现逻辑 -->
6565

6666
```java
67+
import java.util.*;
68+
69+
/**
70+
* @author Furaha Damien
71+
*/
72+
73+
class Solution {
74+
75+
// Helper inner class
76+
public class Point {
77+
int x;
78+
int y;
79+
int distance;
80+
81+
public Point(int x, int y, int distance) {
82+
this.x = x;
83+
this.y = y;
84+
this.distance = distance;
85+
}
86+
}
87+
88+
public int[][] kClosest(int[][] points, int K) {
89+
90+
PriorityQueue<Point> que = new PriorityQueue<Point>((a, b) -> (a.distance - b.distance));
91+
int[][] res = new int[K][2];
92+
93+
for (int[] temp : points) {
94+
int dist = (temp[0] * temp[0] + temp[1] * temp[1]);
95+
que.offer(new Point(temp[0], temp[1], dist));
96+
}
97+
for (int i = 0; i < K; i++) {
98+
Point curr = que.poll();
99+
res[i][0] = curr.x;
100+
res[i][1] = curr.y;
101+
}
102+
return res;
103+
104+
}
105+
}
106+
```
107+
108+
### **TypeScript**
67109

110+
```ts
111+
function kClosest(points: number[][], k: number): number[][] {
112+
return points
113+
.sort((a, b) => a[0] ** 2 + a[1] ** 2 - (b[0] ** 2 + b[1] ** 2))
114+
.slice(0, k);
115+
}
68116
```
69117

70118
### **...**

solution/0900-0999/0973.K Closest Points to Origin/README_EN.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,55 @@ We only want the closest k = 1 points from the origin, so the answer is just [[-
5252
### **Java**
5353

5454
```java
55+
import java.util.*;
56+
57+
/**
58+
* @author Furaha Damien
59+
*/
60+
61+
class Solution {
62+
63+
// Helper inner class
64+
public class Point {
65+
int x;
66+
int y;
67+
int distance;
68+
69+
public Point(int x, int y, int distance) {
70+
this.x = x;
71+
this.y = y;
72+
this.distance = distance;
73+
}
74+
}
75+
76+
public int[][] kClosest(int[][] points, int K) {
77+
78+
PriorityQueue<Point> que = new PriorityQueue<Point>((a, b) -> (a.distance - b.distance));
79+
int[][] res = new int[K][2];
80+
81+
for (int[] temp : points) {
82+
int dist = (temp[0] * temp[0] + temp[1] * temp[1]);
83+
que.offer(new Point(temp[0], temp[1], dist));
84+
}
85+
for (int i = 0; i < K; i++) {
86+
Point curr = que.poll();
87+
res[i][0] = curr.x;
88+
res[i][1] = curr.y;
89+
}
90+
return res;
91+
92+
}
93+
}
94+
```
95+
96+
### **TypeScript**
5597

98+
```ts
99+
function kClosest(points: number[][], k: number): number[][] {
100+
return points
101+
.sort((a, b) => a[0] ** 2 + a[1] ** 2 - (b[0] ** 2 + b[1] ** 2))
102+
.slice(0, k);
103+
}
56104
```
57105

58106
### **...**
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function kClosest(points: number[][], k: number): number[][] {
2+
return points
3+
.sort((a, b) => a[0] ** 2 + a[1] ** 2 - (b[0] ** 2 + b[1] ** 2))
4+
.slice(0, k);
5+
}

0 commit comments

Comments
 (0)