-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem3016.cpp
21 lines (21 loc) · 1023 Bytes
/
problem3016.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int minimumPushes(string word) {
// make freq array for characters
vector<int>letters(26, 0);
for(auto k : word)
letters[k-'a']++;
// Sort in descending order from greatest to smallest frequency
sort(letters.rbegin(), letters.rend());
// Why this sort?
// For example, to make the character with the greatest frequency require only one key press:
// The first 8 characters with the greatest frequency need only one press (i.e., from the first push of the button, the selected character appears on your mobile).
// The second 8 characters require a second press (i.e., from the second push of the button, the selected character appears on your mobile), and so on...
int i = 1, ans = 0;
for(; i <= 3;i++)
for(int j = (i-1)*8; j < i*8;j++)
ans += letters[j] * i;
ans += (letters[24] + letters[25]) * i;
return ans;
}
};