Skip to content

feat: add js/ts solutions to lc problem: No.3016 #3371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -189,6 +189,83 @@ function minimumPushes(word: string): number {
}
```

#### JavaScript

```js
function minimumPushes(word) {
const cnt = Array(26).fill(0);
for (const c of word) {
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
}
cnt.sort((a, b) => b - a);
let ans = 0;
for (let i = 0; i < 26; ++i) {
ans += (((i / 8) | 0) + 1) * cnt[i];
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二:优先队列(大根堆)

<!-- tabs:start -->

#### TypeScript

```ts
function minimumPushes(word: string): number {
const pq = new MaxPriorityQueue();
const cnt = new Map<string, number>();
let [i, res] = [0, 0];

for (const x of word) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}

for (const [x, c] of cnt) {
pq.enqueue(x, c);
}

while (!pq.isEmpty()) {
const c = pq.dequeue().priority;
res += c * (((i++ / 8) | 0) + 1);
}

return res;
}
```

#### JavaScript

```js
function minimumPushes(word) {
const pq = new MaxPriorityQueue();
const cnt = new Map();
let [i, res] = [0, 0];

for (const x of word) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}

for (const [x, c] of cnt) {
pq.enqueue(x, c);
}

while (!pq.isEmpty()) {
const c = pq.dequeue().priority;
res += c * (((i++ / 8) | 0) + 1);
}

return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Original file line number Diff line number Diff line change
@@ -187,6 +187,83 @@ function minimumPushes(word: string): number {
}
```

#### JavaScript

```js
function minimumPushes(word) {
const cnt = Array(26).fill(0);
for (const c of word) {
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
}
cnt.sort((a, b) => b - a);
let ans = 0;
for (let i = 0; i < 26; ++i) {
ans += (((i / 8) | 0) + 1) * cnt[i];
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: Priority Queue

<!-- tabs:start -->

#### TypeScript

```ts
function minimumPushes(word: string): number {
const pq = new MaxPriorityQueue();
const cnt = new Map<string, number>();
let [i, res] = [0, 0];

for (const x of word) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}

for (const [x, c] of cnt) {
pq.enqueue(x, c);
}

while (!pq.isEmpty()) {
const c = pq.dequeue().priority;
res += c * (((i++ / 8) | 0) + 1);
}

return res;
}
```

#### JavaScript

```js
function minimumPushes(word) {
const pq = new MaxPriorityQueue();
const cnt = new Map();
let [i, res] = [0, 0];

for (const x of word) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}

for (const [x, c] of cnt) {
pq.enqueue(x, c);
}

while (!pq.isEmpty()) {
const c = pq.dequeue().priority;
res += c * (((i++ / 8) | 0) + 1);
}

return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function minimumPushes(word) {
const cnt = Array(26).fill(0);
for (const c of word) {
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
}
cnt.sort((a, b) => b - a);
let ans = 0;
for (let i = 0; i < 26; ++i) {
ans += (((i / 8) | 0) + 1) * cnt[i];
}
return ans;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function minimumPushes(word) {
const pq = new MaxPriorityQueue();
const cnt = new Map();
let [i, res] = [0, 0];

for (const x of word) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}

for (const [x, c] of cnt) {
pq.enqueue(x, c);
}

while (!pq.isEmpty()) {
const c = pq.dequeue().priority;
res += c * (((i++ / 8) | 0) + 1);
}

return res;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function minimumPushes(word: string): number {
const pq = new MaxPriorityQueue();
const cnt = new Map<string, number>();
let [i, res] = [0, 0];

for (const x of word) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}

for (const [x, c] of cnt) {
pq.enqueue(x, c);
}

while (!pq.isEmpty()) {
const c = pq.dequeue().priority;
res += c * (((i++ / 8) | 0) + 1);
}

return res;
}