Skip to content

feat: add solutions to lc problem: No.2268 #3262

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 1 commit into from
Jul 12, 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
81 changes: 56 additions & 25 deletions solution/2200-2299/2268.Minimum Number of Keypresses/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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$。

<!-- tabs:start -->

#### Python3
Expand All @@ -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
```

Expand All @@ -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;
Expand All @@ -125,13 +131,17 @@ class Solution {
class Solution {
public:
int minimumKeypresses(string s) {
vector<int> 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>());
int ans = 0, k = 1;
for (int i = 1; i <= 26; ++i) {
ans += k * cnt[i - 1];
if (i % 9 == 0) {
++k;
}
}
return ans;
}
Expand All @@ -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;
}
```

Expand Down
83 changes: 57 additions & 26 deletions solution/2200-2299/2268.Minimum Number of Keypresses/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@ A total of 15 button presses are needed, so return 15.

<!-- solution:start -->

### 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$.

<!-- tabs:start -->

Expand All @@ -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
```

Expand All @@ -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;
Expand All @@ -124,13 +130,17 @@ class Solution {
class Solution {
public:
int minimumKeypresses(string s) {
vector<int> 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>());
int ans = 0, k = 1;
for (int i = 1; i <= 26; ++i) {
ans += k * cnt[i - 1];
if (i % 9 == 0) {
++k;
}
}
return ans;
}
Expand All @@ -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;
}
```

Expand Down
18 changes: 11 additions & 7 deletions solution/2200-2299/2268.Minimum Number of Keypresses/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
class Solution {
public:
int minimumKeypresses(string s) {
vector<int> 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>());
int ans = 0, k = 1;
for (int i = 1; i <= 26; ++i) {
ans += k * cnt[i - 1];
if (i % 9 == 0) {
++k;
}
}
return ans;
}
Expand Down
12 changes: 6 additions & 6 deletions solution/2200-2299/2268.Minimum Number of Keypresses/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
10 changes: 4 additions & 6 deletions solution/2200-2299/2268.Minimum Number of Keypresses/Solution.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions solution/2200-2299/2268.Minimum Number of Keypresses/Solution.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading
Loading