File tree 6 files changed +194
-2
lines changed
solution/2200-2299/2268.Minimum Number of Keypresses
6 files changed +194
-2
lines changed Original file line number Diff line number Diff line change @@ -61,22 +61,90 @@ A total of 15 button presses are needed, so return 15.
61
61
62
62
<!-- 这里可写通用的实现逻辑 -->
63
63
64
+ ** 方法一:计数 + 贪心**
65
+
64
66
<!-- tabs:start -->
65
67
66
68
### ** Python3**
67
69
68
70
<!-- 这里可写当前语言的特殊实现逻辑 -->
69
71
70
72
``` 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
72
84
```
73
85
74
86
### ** Java**
75
87
76
88
<!-- 这里可写当前语言的特殊实现逻辑 -->
77
89
78
90
``` 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
+ ```
79
129
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
+ }
80
148
```
81
149
82
150
### ** TypeScript**
Original file line number Diff line number Diff line change @@ -62,13 +62,79 @@ A total of 15 button presses are needed, so return 15.
62
62
### ** Python3**
63
63
64
64
``` 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
66
76
```
67
77
68
78
### ** Java**
69
79
70
80
``` 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
+ ```
71
119
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
+ }
72
138
```
73
139
74
140
### ** TypeScript**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments