Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3559cc1

Browse files
authored
feat: add js/ts solutions to lc problem: No.3016 (doocs#3371)
1 parent be9f146 commit 3559cc1

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,83 @@ function minimumPushes(word: string): number {
189189
}
190190
```
191191

192+
#### JavaScript
193+
194+
```js
195+
function minimumPushes(word) {
196+
const cnt = Array(26).fill(0);
197+
for (const c of word) {
198+
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
199+
}
200+
cnt.sort((a, b) => b - a);
201+
let ans = 0;
202+
for (let i = 0; i < 26; ++i) {
203+
ans += (((i / 8) | 0) + 1) * cnt[i];
204+
}
205+
return ans;
206+
}
207+
```
208+
209+
<!-- tabs:end -->
210+
211+
<!-- solution:end -->
212+
213+
<!-- solution:start -->
214+
215+
### 方法二:优先队列(大根堆)
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+
#### JavaScript
245+
246+
```js
247+
function minimumPushes(word) {
248+
const pq = new MaxPriorityQueue();
249+
const cnt = new Map();
250+
let [i, res] = [0, 0];
251+
252+
for (const x of word) {
253+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
254+
}
255+
256+
for (const [x, c] of cnt) {
257+
pq.enqueue(x, c);
258+
}
259+
260+
while (!pq.isEmpty()) {
261+
const c = pq.dequeue().priority;
262+
res += c * (((i++ / 8) | 0) + 1);
263+
}
264+
265+
return res;
266+
}
267+
```
268+
192269
<!-- tabs:end -->
193270
194271
<!-- solution:end -->

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,83 @@ function minimumPushes(word: string): number {
187187
}
188188
```
189189

190+
#### JavaScript
191+
192+
```js
193+
function minimumPushes(word) {
194+
const cnt = Array(26).fill(0);
195+
for (const c of word) {
196+
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
197+
}
198+
cnt.sort((a, b) => b - a);
199+
let ans = 0;
200+
for (let i = 0; i < 26; ++i) {
201+
ans += (((i / 8) | 0) + 1) * cnt[i];
202+
}
203+
return ans;
204+
}
205+
```
206+
207+
<!-- tabs:end -->
208+
209+
<!-- solution:end -->
210+
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+
#### JavaScript
243+
244+
```js
245+
function minimumPushes(word) {
246+
const pq = new MaxPriorityQueue();
247+
const cnt = new Map();
248+
let [i, res] = [0, 0];
249+
250+
for (const x of word) {
251+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
252+
}
253+
254+
for (const [x, c] of cnt) {
255+
pq.enqueue(x, c);
256+
}
257+
258+
while (!pq.isEmpty()) {
259+
const c = pq.dequeue().priority;
260+
res += c * (((i++ / 8) | 0) + 1);
261+
}
262+
263+
return res;
264+
}
265+
```
266+
190267
<!-- tabs:end -->
191268
192269
<!-- solution:end -->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function minimumPushes(word) {
2+
const cnt = Array(26).fill(0);
3+
for (const c of word) {
4+
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
5+
}
6+
cnt.sort((a, b) => b - a);
7+
let ans = 0;
8+
for (let i = 0; i < 26; ++i) {
9+
ans += (((i / 8) | 0) + 1) * cnt[i];
10+
}
11+
return ans;
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function minimumPushes(word) {
2+
const pq = new MaxPriorityQueue();
3+
const cnt = new Map();
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+
}
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)

AltStyle によって変換されたページ (->オリジナル) /