File tree 7 files changed +179
-8
lines changed
solution/3000-3099/3014.Minimum Number of Pushes to Type Word I
7 files changed +179
-8
lines changed Original file line number Diff line number Diff line change 65
65
66
66
## 解法
67
67
68
- ### 方法一
68
+ ### 方法一:贪心
69
+
70
+ 我们注意到,字符串 $word$ 中的所有字母都是不同的,因此,我们贪心地将字母均匀地分配到 $8$ 个按键上,即可使得按键次数最少。
71
+
72
+ 时间复杂度 $O(n / 8)$,其中 $n$ 是字符串 $word$ 的长度。空间复杂度 $O(1)$。
69
73
70
74
<!-- tabs:start -->
71
75
72
76
``` 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
74
86
```
75
87
76
88
``` 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
+ }
78
101
```
79
102
80
103
``` 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
+ };
82
117
```
83
118
84
119
```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
+ ```
85
131
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
+ }
86
144
```
87
145
88
146
<!-- tabs: end -->
Original file line number Diff line number Diff line change @@ -61,24 +61,82 @@ It can be shown that no other mapping can provide a lower cost.
61
61
62
62
## Solutions
63
63
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)$.
65
69
66
70
<!-- tabs:start -->
67
71
68
72
``` 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
70
82
```
71
83
72
84
``` 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
+ }
74
97
```
75
98
76
99
``` 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
+ };
78
113
```
79
114
80
115
```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
+ ```
81
127
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
+ }
82
140
```
83
141
84
142
<!-- tabs: end -->
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments