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

Commit 0519581

Browse files
feat: add solutions to lc problem: No.2268 (doocs#3262)
No.2268.Minimum Number of Keypresses
1 parent ec4cae3 commit 0519581

File tree

9 files changed

+174
-94
lines changed

9 files changed

+174
-94
lines changed

‎solution/2200-2299/2268.Minimum Number of Keypresses/README.md‎

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ tags:
7979

8080
### 方法一:计数 + 贪心
8181

82+
我们首先统计字符串 $s$ 中每个字符出现的次数,记录在数组或者哈希表 $\textit{cnt}$ 中。
83+
84+
题目要求按键次数最少,那么出现最多的 9ドル$ 个字符应该对应按键 1ドル$ 到按键 9ドル,ドル出现次数第 10ドル$ 到第 18ドル$ 多的字符再次对应按键 1ドル$ 到按键 9ドル,ドル以此类推。
85+
86+
因此,我们可以将 $\textit{cnt}$ 中的值按照从大到小的顺序排序,然后按照 1ドル$ 到 9ドル$ 的顺序依次分配给按键,每次分配完 9ドル$ 个字符后,按键次数加 1ドル$。
87+
88+
时间复杂度 $O(n + |\Sigma| \times \log |\Sigma|),ドル空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串 $s$ 的长度,而 $\Sigma$ 是字符串 $s$ 中出现的字符集合,本题中 $\Sigma$ 是小写字母集合,因此 $|\Sigma| = 26$。
89+
8290
<!-- tabs:start -->
8391

8492
#### Python3
@@ -87,13 +95,11 @@ tags:
8795
class Solution:
8896
def minimumKeypresses(self, s: str) -> int:
8997
cnt = Counter(s)
90-
ans = 0
91-
i, j = 0, 1
92-
for v in sorted(cnt.values(), reverse=True):
93-
i += 1
94-
ans += j * v
98+
ans, k = 0, 1
99+
for i, x in enumerate(sorted(cnt.values(), reverse=True), 1):
100+
ans += k * x
95101
if i % 9 == 0:
96-
j += 1
102+
k += 1
97103
return ans
98104
```
99105

@@ -103,15 +109,15 @@ class Solution:
103109
class Solution {
104110
public int minimumKeypresses(String s) {
105111
int[] cnt = new int[26];
106-
for (char c : s.toCharArray()) {
107-
++cnt[c - 'a'];
112+
for (int i =0; i < s.length(); ++i) {
113+
++cnt[s.charAt(i) - 'a'];
108114
}
109115
Arrays.sort(cnt);
110-
int ans = 0;
111-
for (int i = 1, j =1; i <= 26; ++i) {
112-
ans += j * cnt[26 - i];
116+
int ans = 0, k =1;
117+
for (int i = 1; i <= 26; ++i) {
118+
ans += k * cnt[26 - i];
113119
if (i % 9 == 0) {
114-
++j;
120+
++k;
115121
}
116122
}
117123
return ans;
@@ -125,13 +131,17 @@ class Solution {
125131
class Solution {
126132
public:
127133
int minimumKeypresses(string s) {
128-
vector<int> cnt(26);
129-
for (char& c : s) ++cnt[c - 'a'];
130-
sort(cnt.begin(), cnt.end());
131-
int ans = 0;
132-
for (int i = 1, j = 1; i <= 26; ++i) {
133-
ans += j * cnt[26 - i];
134-
if (i % 9 == 0) ++j;
134+
int cnt[26]{};
135+
for (char& c : s) {
136+
++cnt[c - 'a'];
137+
}
138+
sort(begin(cnt), end(cnt), greater<int>());
139+
int ans = 0, k = 1;
140+
for (int i = 1; i <= 26; ++i) {
141+
ans += k * cnt[i - 1];
142+
if (i % 9 == 0) {
143+
++k;
144+
}
135145
}
136146
return ans;
137147
}
@@ -141,20 +151,41 @@ public:
141151
#### Go
142152
143153
```go
144-
func minimumKeypresses(s string) int {
154+
func minimumKeypresses(s string) (ans int) {
145155
cnt := make([]int, 26)
146156
for _, c := range s {
147157
cnt[c-'a']++
148158
}
149159
sort.Ints(cnt)
150-
ans := 0
151-
for i, j := 1, 1; i <= 26; i++ {
152-
ans += j * cnt[26-i]
160+
k := 1
161+
for i := 1; i <= 26; i++ {
162+
ans += k * cnt[26-i]
153163
if i%9 == 0 {
154-
j++
164+
k++
155165
}
156166
}
157-
return ans
167+
return
168+
}
169+
```
170+
171+
#### TypeScript
172+
173+
```ts
174+
function minimumKeypresses(s: string): number {
175+
const cnt: number[] = Array(26).fill(0);
176+
const a = 'a'.charCodeAt(0);
177+
for (const c of s) {
178+
++cnt[c.charCodeAt(0) - a];
179+
}
180+
cnt.sort((a, b) => b - a);
181+
let [ans, k] = [0, 1];
182+
for (let i = 1; i <= 26; ++i) {
183+
ans += k * cnt[i - 1];
184+
if (i % 9 === 0) {
185+
++k;
186+
}
187+
}
188+
return ans;
158189
}
159190
```
160191

‎solution/2200-2299/2268.Minimum Number of Keypresses/README_EN.md‎

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ A total of 15 button presses are needed, so return 15.
7676

7777
<!-- solution:start -->
7878

79-
### Solution 1
79+
### Solution 1: Counting + Greedy
80+
81+
First, we count the occurrence of each character in the string $s,ドル and record it in an array or hash table $\textit{cnt}$.
82+
83+
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.
84+
85+
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.
86+
87+
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$.
8088

8189
<!-- tabs:start -->
8290

@@ -86,13 +94,11 @@ A total of 15 button presses are needed, so return 15.
8694
class Solution:
8795
def minimumKeypresses(self, s: str) -> int:
8896
cnt = Counter(s)
89-
ans = 0
90-
i, j = 0, 1
91-
for v in sorted(cnt.values(), reverse=True):
92-
i += 1
93-
ans += j * v
97+
ans, k = 0, 1
98+
for i, x in enumerate(sorted(cnt.values(), reverse=True), 1):
99+
ans += k * x
94100
if i % 9 == 0:
95-
j += 1
101+
k += 1
96102
return ans
97103
```
98104

@@ -102,15 +108,15 @@ class Solution:
102108
class Solution {
103109
public int minimumKeypresses(String s) {
104110
int[] cnt = new int[26];
105-
for (char c : s.toCharArray()) {
106-
++cnt[c - 'a'];
111+
for (int i =0; i < s.length(); ++i) {
112+
++cnt[s.charAt(i) - 'a'];
107113
}
108114
Arrays.sort(cnt);
109-
int ans = 0;
110-
for (int i = 1, j =1; i <= 26; ++i) {
111-
ans += j * cnt[26 - i];
115+
int ans = 0, k =1;
116+
for (int i = 1; i <= 26; ++i) {
117+
ans += k * cnt[26 - i];
112118
if (i % 9 == 0) {
113-
++j;
119+
++k;
114120
}
115121
}
116122
return ans;
@@ -124,13 +130,17 @@ class Solution {
124130
class Solution {
125131
public:
126132
int minimumKeypresses(string s) {
127-
vector<int> cnt(26);
128-
for (char& c : s) ++cnt[c - 'a'];
129-
sort(cnt.begin(), cnt.end());
130-
int ans = 0;
131-
for (int i = 1, j = 1; i <= 26; ++i) {
132-
ans += j * cnt[26 - i];
133-
if (i % 9 == 0) ++j;
133+
int cnt[26]{};
134+
for (char& c : s) {
135+
++cnt[c - 'a'];
136+
}
137+
sort(begin(cnt), end(cnt), greater<int>());
138+
int ans = 0, k = 1;
139+
for (int i = 1; i <= 26; ++i) {
140+
ans += k * cnt[i - 1];
141+
if (i % 9 == 0) {
142+
++k;
143+
}
134144
}
135145
return ans;
136146
}
@@ -140,20 +150,41 @@ public:
140150
#### Go
141151
142152
```go
143-
func minimumKeypresses(s string) int {
153+
func minimumKeypresses(s string) (ans int) {
144154
cnt := make([]int, 26)
145155
for _, c := range s {
146156
cnt[c-'a']++
147157
}
148158
sort.Ints(cnt)
149-
ans := 0
150-
for i, j := 1, 1; i <= 26; i++ {
151-
ans += j * cnt[26-i]
159+
k := 1
160+
for i := 1; i <= 26; i++ {
161+
ans += k * cnt[26-i]
152162
if i%9 == 0 {
153-
j++
163+
k++
154164
}
155165
}
156-
return ans
166+
return
167+
}
168+
```
169+
170+
#### TypeScript
171+
172+
```ts
173+
function minimumKeypresses(s: string): number {
174+
const cnt: number[] = Array(26).fill(0);
175+
const a = 'a'.charCodeAt(0);
176+
for (const c of s) {
177+
++cnt[c.charCodeAt(0) - a];
178+
}
179+
cnt.sort((a, b) => b - a);
180+
let [ans, k] = [0, 1];
181+
for (let i = 1; i <= 26; ++i) {
182+
ans += k * cnt[i - 1];
183+
if (i % 9 === 0) {
184+
++k;
185+
}
186+
}
187+
return ans;
157188
}
158189
```
159190

‎solution/2200-2299/2268.Minimum Number of Keypresses/Solution.cpp‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
class Solution {
22
public:
33
int minimumKeypresses(string s) {
4-
vector<int> cnt(26);
5-
for (char& c : s) ++cnt[c - 'a'];
6-
sort(cnt.begin(), cnt.end());
7-
int ans = 0;
8-
for (int i = 1, j = 1; i <= 26; ++i) {
9-
ans += j * cnt[26 - i];
10-
if (i % 9 == 0) ++j;
4+
int cnt[26]{};
5+
for (char& c : s) {
6+
++cnt[c - 'a'];
7+
}
8+
sort(begin(cnt), end(cnt), greater<int>());
9+
int ans = 0, k = 1;
10+
for (int i = 1; i <= 26; ++i) {
11+
ans += k * cnt[i - 1];
12+
if (i % 9 == 0) {
13+
++k;
14+
}
1115
}
1216
return ans;
1317
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
func minimumKeypresses(s string) int {
1+
func minimumKeypresses(s string) (ansint) {
22
cnt := make([]int, 26)
33
for _, c := range s {
44
cnt[c-'a']++
55
}
66
sort.Ints(cnt)
7-
ans := 0
8-
for i, j:=1, 1; i <= 26; i++ {
9-
ans += j * cnt[26-i]
7+
k := 1
8+
for i:= 1; i <= 26; i++ {
9+
ans += k * cnt[26-i]
1010
if i%9 == 0 {
11-
j++
11+
k++
1212
}
1313
}
14-
returnans
14+
return
1515
}

‎solution/2200-2299/2268.Minimum Number of Keypresses/Solution.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution {
22
public int minimumKeypresses(String s) {
33
int[] cnt = new int[26];
4-
for (charc : s.toCharArray()) {
5-
++cnt[c - 'a'];
4+
for (inti = 0; i < s.length(); ++i) {
5+
++cnt[s.charAt(i) - 'a'];
66
}
77
Arrays.sort(cnt);
8-
int ans = 0;
9-
for (int i = 1, j = 1; i <= 26; ++i) {
10-
ans += j * cnt[26 - i];
8+
int ans = 0, k = 1;
9+
for (int i = 1; i <= 26; ++i) {
10+
ans += k * cnt[26 - i];
1111
if (i % 9 == 0) {
12-
++j;
12+
++k;
1313
}
1414
}
1515
return ans;
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
class Solution:
22
def minimumKeypresses(self, s: str) -> int:
33
cnt = Counter(s)
4-
ans = 0
5-
i, j = 0, 1
6-
for v in sorted(cnt.values(), reverse=True):
7-
i += 1
8-
ans += j * v
4+
ans, k = 0, 1
5+
for i, x in enumerate(sorted(cnt.values(), reverse=True), 1):
6+
ans += k * x
97
if i % 9 == 0:
10-
j += 1
8+
k += 1
119
return ans
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function minimumKeypresses(s: string): number {
2+
const cnt: number[] = Array(26).fill(0);
3+
const a = 'a'.charCodeAt(0);
4+
for (const c of s) {
5+
++cnt[c.charCodeAt(0) - a];
6+
}
7+
cnt.sort((a, b) => b - a);
8+
let [ans, k] = [0, 1];
9+
for (let i = 1; i <= 26; ++i) {
10+
ans += k * cnt[i - 1];
11+
if (i % 9 === 0) {
12+
++k;
13+
}
14+
}
15+
return ans;
16+
}

0 commit comments

Comments
(0)

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