Skip to content

Commit f2da583

Browse files
authored
Create minimum-number-of-keypresses.cpp
1 parent ebe7999 commit f2da583

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

C++/minimum-number-of-keypresses.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// greedy, sort
5+
class Solution {
6+
public:
7+
int minimumKeypresses(string s) {
8+
vector<int> cnt(26);
9+
for (const auto& c : s) {
10+
++cnt[c - 'a'];
11+
}
12+
sort(rbegin(cnt), rend(cnt));
13+
int result = 0;
14+
for (int i = 0; i < size(cnt); ++i) {
15+
result += cnt[i] * (i / 9 + 1);
16+
}
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)