From d2cee185750f20e1ecc3319fd4ddc94fe27d0c4f Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 12 Jul 2024 17:48:48 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2268 No.2268.Minimum Number of Keypresses --- .../README.md | 81 ++++++++++++------ .../README_EN.md | 83 +++++++++++++------ .../Solution.cpp | 18 ++-- .../Solution.go | 12 +-- .../Solution.java | 12 +-- .../Solution.py | 10 +-- .../Solution.ts | 16 ++++ .../README.md | 34 ++++---- solution/README.md | 2 +- 9 files changed, 174 insertions(+), 94 deletions(-) create mode 100644 solution/2200-2299/2268.Minimum Number of Keypresses/Solution.ts diff --git a/solution/2200-2299/2268.Minimum Number of Keypresses/README.md b/solution/2200-2299/2268.Minimum Number of Keypresses/README.md index 950623f6d487c..3d39c60a82d86 100644 --- a/solution/2200-2299/2268.Minimum Number of Keypresses/README.md +++ b/solution/2200-2299/2268.Minimum Number of Keypresses/README.md @@ -79,6 +79,14 @@ tags: ### 方法一:计数 + 贪心 +我们首先统计字符串 $s$ 中每个字符出现的次数,记录在数组或者哈希表 $\textit{cnt}$ 中。 + +题目要求按键次数最少,那么出现最多的 $9$ 个字符应该对应按键 $1$ 到按键 $9$,出现次数第 $10$ 到第 $18$ 多的字符再次对应按键 $1$ 到按键 $9$,以此类推。 + +因此,我们可以将 $\textit{cnt}$ 中的值按照从大到小的顺序排序,然后按照 $1$ 到 $9$ 的顺序依次分配给按键,每次分配完 $9$ 个字符后,按键次数加 $1$。 + +时间复杂度 $O(n + |\Sigma| \times \log |\Sigma|)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串 $s$ 的长度,而 $\Sigma$ 是字符串 $s$ 中出现的字符集合,本题中 $\Sigma$ 是小写字母集合,因此 $|\Sigma| = 26$。 + #### Python3 @@ -87,13 +95,11 @@ tags: class Solution: def minimumKeypresses(self, s: str) -> int: cnt = Counter(s) - ans = 0 - i, j = 0, 1 - for v in sorted(cnt.values(), reverse=True): - i += 1 - ans += j * v + ans, k = 0, 1 + for i, x in enumerate(sorted(cnt.values(), reverse=True), 1): + ans += k * x if i % 9 == 0: - j += 1 + k += 1 return ans ``` @@ -103,15 +109,15 @@ class Solution: class Solution { public int minimumKeypresses(String s) { int[] cnt = new int[26]; - for (char c : s.toCharArray()) { - ++cnt[c - 'a']; + for (int i = 0; i < s.length(); ++i) { + ++cnt[s.charAt(i) - 'a']; } Arrays.sort(cnt); - int ans = 0; - for (int i = 1, j = 1; i <= 26; ++i) { - ans += j * cnt[26 - i]; + int ans = 0, k = 1; + for (int i = 1; i <= 26; ++i) { + ans += k * cnt[26 - i]; if (i % 9 == 0) { - ++j; + ++k; } } return ans; @@ -125,13 +131,17 @@ class Solution { class Solution { public: int minimumKeypresses(string s) { - vector cnt(26); - for (char& c : s) ++cnt[c - 'a']; - sort(cnt.begin(), cnt.end()); - int ans = 0; - for (int i = 1, j = 1; i <= 26; ++i) { - ans += j * cnt[26 - i]; - if (i % 9 == 0) ++j; + int cnt[26]{}; + for (char& c : s) { + ++cnt[c - 'a']; + } + sort(begin(cnt), end(cnt), greater()); + int ans = 0, k = 1; + for (int i = 1; i <= 26; ++i) { + ans += k * cnt[i - 1]; + if (i % 9 == 0) { + ++k; + } } return ans; } @@ -141,20 +151,41 @@ public: #### Go ```go -func minimumKeypresses(s string) int { +func minimumKeypresses(s string) (ans int) { cnt := make([]int, 26) for _, c := range s { cnt[c-'a']++ } sort.Ints(cnt) - ans := 0 - for i, j := 1, 1; i <= 26; i++ { - ans += j * cnt[26-i] + k := 1 + for i := 1; i <= 26; i++ { + ans += k * cnt[26-i] if i%9 == 0 { - j++ + k++ } } - return ans + return +} +``` + +#### TypeScript + +```ts +function minimumKeypresses(s: string): number { + const cnt: number[] = Array(26).fill(0); + const a = 'a'.charCodeAt(0); + for (const c of s) { + ++cnt[c.charCodeAt(0) - a]; + } + cnt.sort((a, b) => b - a); + let [ans, k] = [0, 1]; + for (let i = 1; i <= 26; ++i) { + ans += k * cnt[i - 1]; + if (i % 9 === 0) { + ++k; + } + } + return ans; } ``` diff --git a/solution/2200-2299/2268.Minimum Number of Keypresses/README_EN.md b/solution/2200-2299/2268.Minimum Number of Keypresses/README_EN.md index 72af1ff29d8bb..8d71c0e7bae87 100644 --- a/solution/2200-2299/2268.Minimum Number of Keypresses/README_EN.md +++ b/solution/2200-2299/2268.Minimum Number of Keypresses/README_EN.md @@ -76,7 +76,15 @@ A total of 15 button presses are needed, so return 15. -### Solution 1 +### Solution 1: Counting + Greedy + +First, we count the occurrence of each character in the string $s$, and record it in an array or hash table $\textit{cnt}$. + +The problem requires minimizing the number of key presses, so the $9$ most frequent characters should correspond to keys $1$ to $9$, the $10$th to $18$th most frequent characters should correspond to keys $1$ to $9$ again, and so on. + +Therefore, we can sort the values in $\textit{cnt}$ in descending order, and then allocate them to the keys in the order from $1$ to $9$, adding $1$ to the number of key presses after allocating every $9$ characters. + +The time complexity is $O(n + |\Sigma| \times \log |\Sigma|)$, and the space complexity is $O(|\Sigma|)$. Here, $n$ is the length of the string $s$, and $\Sigma$ is the set of characters appearing in the string $s$. In this problem, $\Sigma$ is the set of lowercase letters, so $|\Sigma| = 26$. @@ -86,13 +94,11 @@ A total of 15 button presses are needed, so return 15. class Solution: def minimumKeypresses(self, s: str) -> int: cnt = Counter(s) - ans = 0 - i, j = 0, 1 - for v in sorted(cnt.values(), reverse=True): - i += 1 - ans += j * v + ans, k = 0, 1 + for i, x in enumerate(sorted(cnt.values(), reverse=True), 1): + ans += k * x if i % 9 == 0: - j += 1 + k += 1 return ans ``` @@ -102,15 +108,15 @@ class Solution: class Solution { public int minimumKeypresses(String s) { int[] cnt = new int[26]; - for (char c : s.toCharArray()) { - ++cnt[c - 'a']; + for (int i = 0; i < s.length(); ++i) { + ++cnt[s.charAt(i) - 'a']; } Arrays.sort(cnt); - int ans = 0; - for (int i = 1, j = 1; i <= 26; ++i) { - ans += j * cnt[26 - i]; + int ans = 0, k = 1; + for (int i = 1; i <= 26; ++i) { + ans += k * cnt[26 - i]; if (i % 9 == 0) { - ++j; + ++k; } } return ans; @@ -124,13 +130,17 @@ class Solution { class Solution { public: int minimumKeypresses(string s) { - vector cnt(26); - for (char& c : s) ++cnt[c - 'a']; - sort(cnt.begin(), cnt.end()); - int ans = 0; - for (int i = 1, j = 1; i <= 26; ++i) { - ans += j * cnt[26 - i]; - if (i % 9 == 0) ++j; + int cnt[26]{}; + for (char& c : s) { + ++cnt[c - 'a']; + } + sort(begin(cnt), end(cnt), greater()); + int ans = 0, k = 1; + for (int i = 1; i <= 26; ++i) { + ans += k * cnt[i - 1]; + if (i % 9 == 0) { + ++k; + } } return ans; } @@ -140,20 +150,41 @@ public: #### Go ```go -func minimumKeypresses(s string) int { +func minimumKeypresses(s string) (ans int) { cnt := make([]int, 26) for _, c := range s { cnt[c-'a']++ } sort.Ints(cnt) - ans := 0 - for i, j := 1, 1; i <= 26; i++ { - ans += j * cnt[26-i] + k := 1 + for i := 1; i <= 26; i++ { + ans += k * cnt[26-i] if i%9 == 0 { - j++ + k++ } } - return ans + return +} +``` + +#### TypeScript + +```ts +function minimumKeypresses(s: string): number { + const cnt: number[] = Array(26).fill(0); + const a = 'a'.charCodeAt(0); + for (const c of s) { + ++cnt[c.charCodeAt(0) - a]; + } + cnt.sort((a, b) => b - a); + let [ans, k] = [0, 1]; + for (let i = 1; i <= 26; ++i) { + ans += k * cnt[i - 1]; + if (i % 9 === 0) { + ++k; + } + } + return ans; } ``` diff --git a/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.cpp b/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.cpp index d7c311261b85d..9059bfc22fd03 100644 --- a/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.cpp +++ b/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.cpp @@ -1,13 +1,17 @@ class Solution { public: int minimumKeypresses(string s) { - vector cnt(26); - for (char& c : s) ++cnt[c - 'a']; - sort(cnt.begin(), cnt.end()); - int ans = 0; - for (int i = 1, j = 1; i <= 26; ++i) { - ans += j * cnt[26 - i]; - if (i % 9 == 0) ++j; + int cnt[26]{}; + for (char& c : s) { + ++cnt[c - 'a']; + } + sort(begin(cnt), end(cnt), greater()); + int ans = 0, k = 1; + for (int i = 1; i <= 26; ++i) { + ans += k * cnt[i - 1]; + if (i % 9 == 0) { + ++k; + } } return ans; } diff --git a/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.go b/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.go index a5dc277cbc03d..cb445880e4228 100644 --- a/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.go +++ b/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.go @@ -1,15 +1,15 @@ -func minimumKeypresses(s string) int { +func minimumKeypresses(s string) (ans int) { cnt := make([]int, 26) for _, c := range s { cnt[c-'a']++ } sort.Ints(cnt) - ans := 0 - for i, j := 1, 1; i <= 26; i++ { - ans += j * cnt[26-i] + k := 1 + for i := 1; i <= 26; i++ { + ans += k * cnt[26-i] if i%9 == 0 { - j++ + k++ } } - return ans + return } \ No newline at end of file diff --git a/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.java b/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.java index 73085c11d41ee..f38f87a71ad2e 100644 --- a/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.java +++ b/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.java @@ -1,15 +1,15 @@ class Solution { public int minimumKeypresses(String s) { int[] cnt = new int[26]; - for (char c : s.toCharArray()) { - ++cnt[c - 'a']; + for (int i = 0; i < s.length(); ++i) { + ++cnt[s.charAt(i) - 'a']; } Arrays.sort(cnt); - int ans = 0; - for (int i = 1, j = 1; i <= 26; ++i) { - ans += j * cnt[26 - i]; + int ans = 0, k = 1; + for (int i = 1; i <= 26; ++i) { + ans += k * cnt[26 - i]; if (i % 9 == 0) { - ++j; + ++k; } } return ans; diff --git a/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.py b/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.py index 480be9bddfe92..531ce9c53b7dc 100644 --- a/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.py +++ b/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.py @@ -1,11 +1,9 @@ class Solution: def minimumKeypresses(self, s: str) -> int: cnt = Counter(s) - ans = 0 - i, j = 0, 1 - for v in sorted(cnt.values(), reverse=True): - i += 1 - ans += j * v + ans, k = 0, 1 + for i, x in enumerate(sorted(cnt.values(), reverse=True), 1): + ans += k * x if i % 9 == 0: - j += 1 + k += 1 return ans diff --git a/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.ts b/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.ts new file mode 100644 index 0000000000000..3411eae4992b4 --- /dev/null +++ b/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.ts @@ -0,0 +1,16 @@ +function minimumKeypresses(s: string): number { + const cnt: number[] = Array(26).fill(0); + const a = 'a'.charCodeAt(0); + for (const c of s) { + ++cnt[c.charCodeAt(0) - a]; + } + cnt.sort((a, b) => b - a); + let [ans, k] = [0, 1]; + for (let i = 1; i <= 26; ++i) { + ans += k * cnt[i - 1]; + if (i % 9 === 0) { + ++k; + } + } + return ans; +} diff --git a/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/README.md b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/README.md index ebb4b15a1117e..ce6df124b9518 100644 --- a/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/README.md +++ b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/README.md @@ -6,7 +6,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3215.Co -# [3215. Count Triplets with Even XOR Set Bits II 🔒](https://leetcode.cn/problems/count-triplets-with-even-xor-set-bits-ii) +# [3215. 用偶数异或设置位计数三元组 II 🔒](https://leetcode.cn/problems/count-triplets-with-even-xor-set-bits-ii) [English Version](/solution/3200-3299/3215.Count%20Triplets%20with%20Even%20XOR%20Set%20Bits%20II/README_EN.md) @@ -14,31 +14,29 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3215.Co -Given three integer arrays a, b, and c, return the number of triplets (a[i], b[j], c[k]), such that the bitwise XOR between the elements of each triplet has an even number of set bits. +

给定三个整数数组 ab 和 c,返回组内元素按位 XOR 有 偶数 个 设置位 的三元组 (a[i], b[j], c[k]) 的数量。

 

-

Example 1:

-
-

Input: a = [1], b = [2], c = [3]

+

示例 1:

-

Output: 1

+

输入:a = [1], b = [2], c = [3]

-

Explanation:

+

输出:1

-

The only triplet is (a[0], b[0], c[0]) and their XOR is: 1 XOR 2 XOR 3 = 002.

-
+

解释:

-

Example 2:

+

只有一个三元组 (a[0], b[0], c[0]) 并且它们的 XOR 为:1 XOR 2 XOR 3 = 002

-
-

Input: a = [1,1], b = [2,3], c = [1,5]

+

示例 2:

-

Output: 4

+

输入:a = [1,1], b = [2,3], c = [1,5]

-

Explanation:

+

输出:4

-

Consider these four triplets:

+

解释:

+ +

考虑以下 4 个三元组:

  • (a[0], b[1], c[0]): 1 XOR 3 XOR 1 = 0112
  • @@ -46,16 +44,18 @@ Given three integer arrays a, b, and c, r
  • (a[0], b[0], c[1]): 1 XOR 2 XOR 5 = 1102
  • (a[1], b[0], c[1]): 1 XOR 2 XOR 5 = 1102
-

 

-

Constraints:

+ +

提示:

  • 1 <= a.length, b.length, c.length <= 105
  • 0 <= a[i], b[i], c[i] <= 109
+

 

+ ## 解法 diff --git a/solution/README.md b/solution/README.md index bd8fe337a5b1b..9a26e3b72cde8 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3225,7 +3225,7 @@ | 3212 | [统计 X 和 Y 频数相等的子矩阵数量](/solution/3200-3299/3212.Count%20Submatrices%20With%20Equal%20Frequency%20of%20X%20and%20Y/README.md) | `数组`,`矩阵`,`前缀和` | 中等 | 第 405 场周赛 | | 3213 | [最小代价构造字符串](/solution/3200-3299/3213.Construct%20String%20with%20Minimum%20Cost/README.md) | `数组`,`字符串`,`动态规划`,`后缀数组` | 困难 | 第 405 场周赛 | | 3214 | [同比增长率](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md) | `数据库` | 困难 | 🔒 | -| 3215 | [Count Triplets with Even XOR Set Bits II](/solution/3200-3299/3215.Count%20Triplets%20with%20Even%20XOR%20Set%20Bits%20II/README.md) | | 中等 | 🔒 | +| 3215 | [用偶数异或设置位计数三元组 II](/solution/3200-3299/3215.Count%20Triplets%20with%20Even%20XOR%20Set%20Bits%20II/README.md) | | 中等 | 🔒 | ## 版权