Skip to content

Commit

Permalink
Create 3016. Minimum Number of Pushes to Type Word II (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Aug 6, 2024
2 parents 696ef67 + 4af0cc0 commit d7692d7
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 3016. Minimum Number of Pushes to Type Word II
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public:
// bool ccompare( pair<char, int>& a, pair<char, int>& b) {
// return a.second > b.second;
// }
int minimumPushes(string word) {
vector<pair<char, int>> freq(26);
// initialising a vector of pairs which will store the alphabet and its
// corresponding frequency
for (int i = 0; i < 26; i++) {
freq[i].first = 'a' + i;
freq[i].second = 0;
}
// counting an aplhabets frequency
for (int i = 0; i < word.length(); i++) {
if (word[i] >= 'a' && word[i] <= 'z') {
freq[word[i] - 'a'].second++;
}
}
// sorting it in decreasing order (priorty to alphabet with high
// frequency)
sort(freq.begin(), freq.end(),
[](pair<char, int>& a, pair<char, int>& b) {
return a.second > b.second;
});
int push = 0;
for (int i = 0; i < freq.size(); i++) {
if (freq[i].second == 0)
break;
push += freq[i].second * ((i / 8) + 1);
// we have 8 keys available (2 to 9), so every 8 characters, we
// increase the number of pushes needed.
}
return push;
}
};

0 comments on commit d7692d7

Please sign in to comment.