From 8c0ca37a69783f2945fb3dc6198ea5c5a4fd1a5b Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 6 Aug 2024 14:56:48 +0300 Subject: [PATCH 1/4] feat: add js solution to lc problem: No.3016 --- .../README.md | 17 +++++++++++++++++ .../README_EN.md | 17 +++++++++++++++++ .../Solution.js | 12 ++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/Solution.js diff --git a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md index 7e0a1dab7f5b4..789f5bacaee43 100644 --- a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md +++ b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md @@ -189,6 +189,23 @@ 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; +} +``` + diff --git a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md index ed9a33d2f0aae..0c199c4d06429 100644 --- a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md +++ b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md @@ -187,6 +187,23 @@ 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; +} +``` + diff --git a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/Solution.js b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/Solution.js new file mode 100644 index 0000000000000..67bebe50efead --- /dev/null +++ b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/Solution.js @@ -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; +} From 092341a072e72be036c12f9e8cfe7be4bdb75930 Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 6 Aug 2024 14:59:41 +0300 Subject: [PATCH 2/4] feat: add ts solution to lc problem: No.3016 --- .../README.md | 35 +++++++++++++++++++ .../README_EN.md | 35 +++++++++++++++++++ .../Solution2.ts | 20 +++++++++++ 3 files changed, 90 insertions(+) create mode 100644 solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/Solution2.ts diff --git a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md index 789f5bacaee43..cadb39a485d78 100644 --- a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md +++ b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md @@ -210,4 +210,39 @@ function minimumPushes(word) { + + +### Solution 2: Priority Queue + + + +#### TypeScript + +```ts +function minimumPushes(word: string): number { + 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; +} +``` + + + + + diff --git a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md index 0c199c4d06429..8cf140e28bbf7 100644 --- a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md +++ b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md @@ -208,4 +208,39 @@ function minimumPushes(word) { + + +### Solution 2: Priority Queue + + + +#### TypeScript + +```ts +function minimumPushes(word: string): number { + 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; +} +``` + + + + + diff --git a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/Solution2.ts b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/Solution2.ts new file mode 100644 index 0000000000000..7a7be327aadc7 --- /dev/null +++ b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/Solution2.ts @@ -0,0 +1,20 @@ +function minimumPushes(word: string): number { + 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; +} From 313a008e1f75707e1507b4ba267d9902300c7dc9 Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 6 Aug 2024 15:00:55 +0300 Subject: [PATCH 3/4] feat: add js solution to lc problem: No.3016 --- .../README.md | 25 +++++++++++++++++++ .../README_EN.md | 25 +++++++++++++++++++ .../Solution2.js | 20 +++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/Solution2.js diff --git a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md index cadb39a485d78..eb4818926ea7b 100644 --- a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md +++ b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md @@ -241,6 +241,31 @@ function minimumPushes(word: string): number { } ``` +#### 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; +} +``` + diff --git a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md index 8cf140e28bbf7..1d470861064a2 100644 --- a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md +++ b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md @@ -239,6 +239,31 @@ function minimumPushes(word: string): number { } ``` +#### 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; +} +``` + diff --git a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/Solution2.js b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/Solution2.js new file mode 100644 index 0000000000000..f98f1bee76363 --- /dev/null +++ b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/Solution2.js @@ -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; +} From e9fedbfb3c65d607f27d3ae9c78fe99ec624d13e Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 6 Aug 2024 20:05:17 +0800 Subject: [PATCH 4/4] Update README.md --- .../3016.Minimum Number of Pushes to Type Word II/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md index eb4818926ea7b..e11174c7661b8 100644 --- a/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md +++ b/solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md @@ -212,7 +212,7 @@ function minimumPushes(word) { -### Solution 2: Priority Queue +### 方法二:优先队列(大根堆)

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