Skip to content

Commit 98f128c

Browse files
authored
feat: add solutions to lc problem: No.3014 (#2252)
No.3014.Minimum Number of Pushes to Type Word I
1 parent d62fbe3 commit 98f128c

File tree

7 files changed

+179
-8
lines changed

7 files changed

+179
-8
lines changed

solution/3000-3099/3014.Minimum Number of Pushes to Type Word I/README.md

+62-4
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,82 @@
6565

6666
## 解法
6767

68-
### 方法一
68+
### 方法一:贪心
69+
70+
我们注意到,字符串 $word$ 中的所有字母都是不同的,因此,我们贪心地将字母均匀地分配到 $8$ 个按键上,即可使得按键次数最少。
71+
72+
时间复杂度 $O(n / 8)$,其中 $n$ 是字符串 $word$ 的长度。空间复杂度 $O(1)$。
6973

7074
<!-- tabs:start -->
7175

7276
```python
73-
77+
class Solution:
78+
def minimumPushes(self, word: str) -> int:
79+
n = len(word)
80+
ans, k = 0, 1
81+
for _ in range(n // 8):
82+
ans += k * 8
83+
k += 1
84+
ans += k * (n % 8)
85+
return ans
7486
```
7587

7688
```java
77-
89+
class Solution {
90+
public int minimumPushes(String word) {
91+
int n = word.length();
92+
int ans = 0, k = 1;
93+
for (int i = 0; i < n / 8; ++i) {
94+
ans += k * 8;
95+
++k;
96+
}
97+
ans += k * (n % 8);
98+
return ans;
99+
}
100+
}
78101
```
79102

80103
```cpp
81-
104+
class Solution {
105+
public:
106+
int minimumPushes(string word) {
107+
int n = word.size();
108+
int ans = 0, k = 1;
109+
for (int i = 0; i < n / 8; ++i) {
110+
ans += k * 8;
111+
++k;
112+
}
113+
ans += k * (n % 8);
114+
return ans;
115+
}
116+
};
82117
```
83118
84119
```go
120+
func minimumPushes(word string) (ans int) {
121+
n := len(word)
122+
k := 1
123+
for i := 0; i < n/8; i++ {
124+
ans += k * 8
125+
k++
126+
}
127+
ans += k * (n % 8)
128+
return
129+
}
130+
```
85131

132+
```ts
133+
function minimumPushes(word: string): number {
134+
const n = word.length;
135+
let ans = 0;
136+
let k = 1;
137+
for (let i = 0; i < ((n / 8) | 0); ++i) {
138+
ans += k * 8;
139+
++k;
140+
}
141+
ans += k * (n % 8);
142+
return ans;
143+
}
86144
```
87145

88146
<!-- tabs:end -->

solution/3000-3099/3014.Minimum Number of Pushes to Type Word I/README_EN.md

+62-4
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,82 @@ It can be shown that no other mapping can provide a lower cost.
6161

6262
## Solutions
6363

64-
### Solution 1
64+
### Solution 1: Greedy Algorithm
65+
66+
We notice that all the letters in the string $word$ are different. Therefore, we can greedily distribute the letters evenly across the $8$ keys to minimize the number of key presses.
67+
68+
The time complexity is $O(n / 8)$, where $n$ is the length of the string $word$. The space complexity is $O(1)$.
6569

6670
<!-- tabs:start -->
6771

6872
```python
69-
73+
class Solution:
74+
def minimumPushes(self, word: str) -> int:
75+
n = len(word)
76+
ans, k = 0, 1
77+
for _ in range(n // 8):
78+
ans += k * 8
79+
k += 1
80+
ans += k * (n % 8)
81+
return ans
7082
```
7183

7284
```java
73-
85+
class Solution {
86+
public int minimumPushes(String word) {
87+
int n = word.length();
88+
int ans = 0, k = 1;
89+
for (int i = 0; i < n / 8; ++i) {
90+
ans += k * 8;
91+
++k;
92+
}
93+
ans += k * (n % 8);
94+
return ans;
95+
}
96+
}
7497
```
7598

7699
```cpp
77-
100+
class Solution {
101+
public:
102+
int minimumPushes(string word) {
103+
int n = word.size();
104+
int ans = 0, k = 1;
105+
for (int i = 0; i < n / 8; ++i) {
106+
ans += k * 8;
107+
++k;
108+
}
109+
ans += k * (n % 8);
110+
return ans;
111+
}
112+
};
78113
```
79114
80115
```go
116+
func minimumPushes(word string) (ans int) {
117+
n := len(word)
118+
k := 1
119+
for i := 0; i < n/8; i++ {
120+
ans += k * 8
121+
k++
122+
}
123+
ans += k * (n % 8)
124+
return
125+
}
126+
```
81127

128+
```ts
129+
function minimumPushes(word: string): number {
130+
const n = word.length;
131+
let ans = 0;
132+
let k = 1;
133+
for (let i = 0; i < ((n / 8) | 0); ++i) {
134+
ans += k * 8;
135+
++k;
136+
}
137+
ans += k * (n % 8);
138+
return ans;
139+
}
82140
```
83141

84142
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int minimumPushes(string word) {
4+
int n = word.size();
5+
int ans = 0, k = 1;
6+
for (int i = 0; i < n / 8; ++i) {
7+
ans += k * 8;
8+
++k;
9+
}
10+
ans += k * (n % 8);
11+
return ans;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func minimumPushes(word string) (ans int) {
2+
n := len(word)
3+
k := 1
4+
for i := 0; i < n/8; i++ {
5+
ans += k * 8
6+
k++
7+
}
8+
ans += k * (n % 8)
9+
return
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int minimumPushes(String word) {
3+
int n = word.length();
4+
int ans = 0, k = 1;
5+
for (int i = 0; i < n / 8; ++i) {
6+
ans += k * 8;
7+
++k;
8+
}
9+
ans += k * (n % 8);
10+
return ans;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def minimumPushes(self, word: str) -> int:
3+
n = len(word)
4+
ans, k = 0, 1
5+
for _ in range(n // 8):
6+
ans += k * 8
7+
k += 1
8+
ans += k * (n % 8)
9+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function minimumPushes(word: string): number {
2+
const n = word.length;
3+
let ans = 0;
4+
let k = 1;
5+
for (let i = 0; i < ((n / 8) | 0); ++i) {
6+
ans += k * 8;
7+
++k;
8+
}
9+
ans += k * (n % 8);
10+
return ans;
11+
}

0 commit comments

Comments
 (0)