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

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
yanglbme merged 4 commits into doocs:main from rain84:feature/lc-3016
Aug 6, 2024
Merged
Show file tree
Hide file tree
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
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Expand Down
View file Open in desktop
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;
}
View file Open in desktop
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;
}
View file Open in desktop
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;
}

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