Skip to content

Commit 3d022be

Browse files
committed
feat: add solutions to lc problem: No.2268
No.2268.Minimum Number of Keypresses
1 parent 3028770 commit 3d022be

File tree

6 files changed

+194
-2
lines changed

6 files changed

+194
-2
lines changed

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

+69-1
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,90 @@ A total of 15 button presses are needed, so return 15.
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
**方法一:计数 + 贪心**
65+
6466
<!-- tabs:start -->
6567

6668
### **Python3**
6769

6870
<!-- 这里可写当前语言的特殊实现逻辑 -->
6971

7072
```python
71-
73+
class Solution:
74+
def minimumKeypresses(self, s: str) -> int:
75+
cnt = Counter(s)
76+
ans = 0
77+
i, j = 0, 1
78+
for v in sorted(cnt.values(), reverse=True):
79+
i += 1
80+
ans += j * v
81+
if i % 9 == 0:
82+
j += 1
83+
return ans
7284
```
7385

7486
### **Java**
7587

7688
<!-- 这里可写当前语言的特殊实现逻辑 -->
7789

7890
```java
91+
class Solution {
92+
public int minimumKeypresses(String s) {
93+
int[] cnt = new int[26];
94+
for (char c : s.toCharArray()) {
95+
++cnt[c - 'a'];
96+
}
97+
Arrays.sort(cnt);
98+
int ans = 0;
99+
for (int i = 1, j = 1; i <= 26; ++i) {
100+
ans += j * cnt[26 - i];
101+
if (i % 9 == 0) {
102+
++j;
103+
}
104+
}
105+
return ans;
106+
}
107+
}
108+
```
109+
110+
### **C++**
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
int minimumKeypresses(string s) {
116+
vector<int> cnt(26);
117+
for (char& c : s) ++cnt[c - 'a'];
118+
sort(cnt.begin(), cnt.end());
119+
int ans = 0;
120+
for (int i = 1, j = 1; i <= 26; ++i)
121+
{
122+
ans += j * cnt[26 - i];
123+
if (i % 9 == 0) ++j;
124+
}
125+
return ans;
126+
}
127+
};
128+
```
79129
130+
### **Go**
131+
132+
```go
133+
func minimumKeypresses(s string) int {
134+
cnt := make([]int, 26)
135+
for _, c := range s {
136+
cnt[c-'a']++
137+
}
138+
sort.Ints(cnt)
139+
ans := 0
140+
for i, j := 1, 1; i <= 26; i++ {
141+
ans += j * cnt[26-i]
142+
if i%9 == 0 {
143+
j++
144+
}
145+
}
146+
return ans
147+
}
80148
```
81149

82150
### **TypeScript**

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

+67-1
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,79 @@ A total of 15 button presses are needed, so return 15.
6262
### **Python3**
6363

6464
```python
65-
65+
class Solution:
66+
def minimumKeypresses(self, s: str) -> int:
67+
cnt = Counter(s)
68+
ans = 0
69+
i, j = 0, 1
70+
for v in sorted(cnt.values(), reverse=True):
71+
i += 1
72+
ans += j * v
73+
if i % 9 == 0:
74+
j += 1
75+
return ans
6676
```
6777

6878
### **Java**
6979

7080
```java
81+
class Solution {
82+
public int minimumKeypresses(String s) {
83+
int[] cnt = new int[26];
84+
for (char c : s.toCharArray()) {
85+
++cnt[c - 'a'];
86+
}
87+
Arrays.sort(cnt);
88+
int ans = 0;
89+
for (int i = 1, j = 1; i <= 26; ++i) {
90+
ans += j * cnt[26 - i];
91+
if (i % 9 == 0) {
92+
++j;
93+
}
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int minimumKeypresses(string s) {
106+
vector<int> cnt(26);
107+
for (char& c : s) ++cnt[c - 'a'];
108+
sort(cnt.begin(), cnt.end());
109+
int ans = 0;
110+
for (int i = 1, j = 1; i <= 26; ++i)
111+
{
112+
ans += j * cnt[26 - i];
113+
if (i % 9 == 0) ++j;
114+
}
115+
return ans;
116+
}
117+
};
118+
```
71119
120+
### **Go**
121+
122+
```go
123+
func minimumKeypresses(s string) int {
124+
cnt := make([]int, 26)
125+
for _, c := range s {
126+
cnt[c-'a']++
127+
}
128+
sort.Ints(cnt)
129+
ans := 0
130+
for i, j := 1, 1; i <= 26; i++ {
131+
ans += j * cnt[26-i]
132+
if i%9 == 0 {
133+
j++
134+
}
135+
}
136+
return ans
137+
}
72138
```
73139

74140
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
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+
{
10+
ans += j * cnt[26 - i];
11+
if (i % 9 == 0) ++j;
12+
}
13+
return ans;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func minimumKeypresses(s string) int {
2+
cnt := make([]int, 26)
3+
for _, c := range s {
4+
cnt[c-'a']++
5+
}
6+
sort.Ints(cnt)
7+
ans := 0
8+
for i, j := 1, 1; i <= 26; i++ {
9+
ans += j * cnt[26-i]
10+
if i%9 == 0 {
11+
j++
12+
}
13+
}
14+
return ans
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int minimumKeypresses(String s) {
3+
int[] cnt = new int[26];
4+
for (char c : s.toCharArray()) {
5+
++cnt[c - 'a'];
6+
}
7+
Arrays.sort(cnt);
8+
int ans = 0;
9+
for (int i = 1, j = 1; i <= 26; ++i) {
10+
ans += j * cnt[26 - i];
11+
if (i % 9 == 0) {
12+
++j;
13+
}
14+
}
15+
return ans;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def minimumKeypresses(self, s: str) -> int:
3+
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
9+
if i % 9 == 0:
10+
j += 1
11+
return ans

0 commit comments

Comments
 (0)