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 3acf5dd

Browse files
feat: add solutions to lc problem: No.3016 (doocs#2253)
No.3016.Minimum Number of Pushes to Type Word II
1 parent 98f128c commit 3acf5dd

File tree

7 files changed

+191
-8
lines changed

7 files changed

+191
-8
lines changed

‎solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README.md‎

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,86 @@
7777

7878
## 解法
7979

80-
### 方法一
80+
### 方法一:贪心 + 排序
81+
82+
我们用一个哈希表或数组 $cnt$ 统计字符串 $word$ 中每个字母出现的次数。接下来,按照字母出现的次数从大到小排序,然后每 8ドル$ 个字母一组,将每组中的字母分配到 8ドル$ 个按键上。
83+
84+
时间复杂度 $O(n + |\Sigma| \times \log |\Sigma|),ドル空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串 $word$ 的长度,而 $\Sigma$ 是字符串 $word$ 中出现的字母集合。
8185

8286
<!-- tabs:start -->
8387

8488
```python
85-
89+
class Solution:
90+
def minimumPushes(self, word: str) -> int:
91+
cnt = Counter(word)
92+
ans = 0
93+
for i, x in enumerate(sorted(cnt.values(), reverse=True)):
94+
ans += (i // 8 + 1) * x
95+
return ans
8696
```
8797

8898
```java
89-
99+
class Solution {
100+
public int minimumPushes(String word) {
101+
int[] cnt = new int[26];
102+
for (int i = 0; i < word.length(); ++i) {
103+
++cnt[word.charAt(i) - 'a'];
104+
}
105+
Arrays.sort(cnt);
106+
int ans = 0;
107+
for (int i = 0; i < 26; ++i) {
108+
ans += (i / 8 + 1) * cnt[26 - i - 1];
109+
}
110+
return ans;
111+
}
112+
}
90113
```
91114

92115
```cpp
93-
116+
class Solution {
117+
public:
118+
int minimumPushes(string word) {
119+
vector<int> cnt(26);
120+
for (char& c : word) {
121+
++cnt[c - 'a'];
122+
}
123+
sort(cnt.rbegin(), cnt.rend());
124+
int ans = 0;
125+
for (int i = 0; i < 26; ++i) {
126+
ans += (i / 8 + 1) * cnt[i];
127+
}
128+
return ans;
129+
}
130+
};
94131
```
95132
96133
```go
134+
func minimumPushes(word string) (ans int) {
135+
cnt := make([]int, 26)
136+
for _, c := range word {
137+
cnt[c-'a']++
138+
}
139+
sort.Ints(cnt)
140+
for i := 0; i < 26; i++ {
141+
ans += (i/8 + 1) * cnt[26-i-1]
142+
}
143+
return
144+
}
145+
```
97146

147+
```ts
148+
function minimumPushes(word: string): number {
149+
const cnt: number[] = Array(26).fill(0);
150+
for (const c of word) {
151+
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
152+
}
153+
cnt.sort((a, b) => b - a);
154+
let ans = 0;
155+
for (let i = 0; i < 26; ++i) {
156+
ans += (((i / 8) | 0) + 1) * cnt[i];
157+
}
158+
return ans;
159+
}
98160
```
99161

100162
<!-- tabs:end -->

‎solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md‎

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,86 @@ It can be shown that no other mapping can provide a lower cost.
7373

7474
## Solutions
7575

76-
### Solution 1
76+
### Solution 1: Greedy Algorithm + Sorting
77+
78+
We use a hash table or array $cnt$ to count the number of occurrences of each letter in the string $word$. Next, we sort the letters in descending order of their counts, and then group every 8ドル$ letters together, assigning each group to the 8ドル$ keys.
79+
80+
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 $word,ドル and $\Sigma$ is the set of letters that appear in the string $word$.
7781

7882
<!-- tabs:start -->
7983

8084
```python
81-
85+
class Solution:
86+
def minimumPushes(self, word: str) -> int:
87+
cnt = Counter(word)
88+
ans = 0
89+
for i, x in enumerate(sorted(cnt.values(), reverse=True)):
90+
ans += (i // 8 + 1) * x
91+
return ans
8292
```
8393

8494
```java
85-
95+
class Solution {
96+
public int minimumPushes(String word) {
97+
int[] cnt = new int[26];
98+
for (int i = 0; i < word.length(); ++i) {
99+
++cnt[word.charAt(i) - 'a'];
100+
}
101+
Arrays.sort(cnt);
102+
int ans = 0;
103+
for (int i = 0; i < 26; ++i) {
104+
ans += (i / 8 + 1) * cnt[26 - i - 1];
105+
}
106+
return ans;
107+
}
108+
}
86109
```
87110

88111
```cpp
89-
112+
class Solution {
113+
public:
114+
int minimumPushes(string word) {
115+
vector<int> cnt(26);
116+
for (char& c : word) {
117+
++cnt[c - 'a'];
118+
}
119+
sort(cnt.rbegin(), cnt.rend());
120+
int ans = 0;
121+
for (int i = 0; i < 26; ++i) {
122+
ans += (i / 8 + 1) * cnt[i];
123+
}
124+
return ans;
125+
}
126+
};
90127
```
91128
92129
```go
130+
func minimumPushes(word string) (ans int) {
131+
cnt := make([]int, 26)
132+
for _, c := range word {
133+
cnt[c-'a']++
134+
}
135+
sort.Ints(cnt)
136+
for i := 0; i < 26; i++ {
137+
ans += (i/8 + 1) * cnt[26-i-1]
138+
}
139+
return
140+
}
141+
```
93142

143+
```ts
144+
function minimumPushes(word: string): number {
145+
const cnt: number[] = Array(26).fill(0);
146+
for (const c of word) {
147+
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
148+
}
149+
cnt.sort((a, b) => b - a);
150+
let ans = 0;
151+
for (let i = 0; i < 26; ++i) {
152+
ans += (((i / 8) | 0) + 1) * cnt[i];
153+
}
154+
return ans;
155+
}
94156
```
95157

96158
<!-- tabs:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int minimumPushes(string word) {
4+
vector<int> cnt(26);
5+
for (char& c : word) {
6+
++cnt[c - 'a'];
7+
}
8+
sort(cnt.rbegin(), cnt.rend());
9+
int ans = 0;
10+
for (int i = 0; i < 26; ++i) {
11+
ans += (i / 8 + 1) * cnt[i];
12+
}
13+
return ans;
14+
}
15+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func minimumPushes(word string) (ans int) {
2+
cnt := make([]int, 26)
3+
for _, c := range word {
4+
cnt[c-'a']++
5+
}
6+
sort.Ints(cnt)
7+
for i := 0; i < 26; i++ {
8+
ans += (i/8 + 1) * cnt[26-i-1]
9+
}
10+
return
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int minimumPushes(String word) {
3+
int[] cnt = new int[26];
4+
for (int i = 0; i < word.length(); ++i) {
5+
++cnt[word.charAt(i) - 'a'];
6+
}
7+
Arrays.sort(cnt);
8+
int ans = 0;
9+
for (int i = 0; i < 26; ++i) {
10+
ans += (i / 8 + 1) * cnt[26 - i - 1];
11+
}
12+
return ans;
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def minimumPushes(self, word: str) -> int:
3+
cnt = Counter(word)
4+
ans = 0
5+
for i, x in enumerate(sorted(cnt.values(), reverse=True)):
6+
ans += (i // 8 + 1) * x
7+
return ans
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function minimumPushes(word: string): number {
2+
const cnt: number[] = Array(26).fill(0);
3+
for (const c of word) {
4+
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
5+
}
6+
cnt.sort((a, b) => b - a);
7+
let ans = 0;
8+
for (let i = 0; i < 26; ++i) {
9+
ans += (((i / 8) | 0) + 1) * cnt[i];
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
(0)

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