Skip to content

Commit 092341a

Browse files
committed
feat: add ts solution to lc problem: No.3016
1 parent 8c0ca37 commit 092341a

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,39 @@ function minimumPushes(word) {
210210

211211
<!-- solution:end -->
212212

213+
<!-- solution:start -->
214+
215+
### Solution 2: Priority Queue
216+
217+
<!-- tabs:start -->
218+
219+
#### TypeScript
220+
221+
```ts
222+
function minimumPushes(word: string): number {
223+
const pq = new MaxPriorityQueue();
224+
const cnt = new Map<string, number>();
225+
let [i, res] = [0, 0];
226+
227+
for (const x of word) {
228+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
229+
}
230+
231+
for (const [x, c] of cnt) {
232+
pq.enqueue(x, c);
233+
}
234+
235+
while (!pq.isEmpty()) {
236+
const c = pq.dequeue().priority;
237+
res += c * (((i++ / 8) | 0) + 1);
238+
}
239+
240+
return res;
241+
}
242+
```
243+
244+
<!-- tabs:end -->
245+
246+
<!-- solution:end -->
247+
213248
<!-- problem:end -->

solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,39 @@ function minimumPushes(word) {
208208

209209
<!-- solution:end -->
210210

211+
<!-- solution:start -->
212+
213+
### Solution 2: Priority Queue
214+
215+
<!-- tabs:start -->
216+
217+
#### TypeScript
218+
219+
```ts
220+
function minimumPushes(word: string): number {
221+
const pq = new MaxPriorityQueue();
222+
const cnt = new Map<string, number>();
223+
let [i, res] = [0, 0];
224+
225+
for (const x of word) {
226+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
227+
}
228+
229+
for (const [x, c] of cnt) {
230+
pq.enqueue(x, c);
231+
}
232+
233+
while (!pq.isEmpty()) {
234+
const c = pq.dequeue().priority;
235+
res += c * (((i++ / 8) | 0) + 1);
236+
}
237+
238+
return res;
239+
}
240+
```
241+
242+
<!-- tabs:end -->
243+
244+
<!-- solution:end -->
245+
211246
<!-- problem:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function minimumPushes(word: string): number {
2+
const pq = new MaxPriorityQueue();
3+
const cnt = new Map<string, number>();
4+
let [i, res] = [0, 0];
5+
6+
for (const x of word) {
7+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
8+
}
9+
10+
for (const [x, c] of cnt) {
11+
pq.enqueue(x, c);
12+
}
13+
14+
while (!pq.isEmpty()) {
15+
const c = pq.dequeue().priority;
16+
res += c * (((i++ / 8) | 0) + 1);
17+
}
18+
19+
return res;
20+
}

0 commit comments

Comments
 (0)