/**
* Problem: LeetCode 3016 - Minimum Number of Pushes to Type Word II
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.*;
/**
* Problem: LeetCode 3016 - Minimum Number of Pushes to Type Word II
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int minimumPushes(String word) {
int[] freq = new int[26];
for (char c : word.toCharArray()) {
freq[c - 'a']++;
}
Integer[] idx = new Integer[26];
for (int i = 0; i < 26; i++) idx[i] = i;
Arrays.sort(idx, (a, b) -> freq[b] - freq[a]);
int totalPushes = 0;
for (int i = 0; i < 26; i++) {
if (freq[idx[i]] == 0) break;
int pushCount = (i / 8) + 1;
totalPushes += pushCount * freq[idx[i]];
}
return totalPushes;
}
}
"""
Problem: LeetCode 3016 - Minimum Number of Pushes to Type Word II
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from collections import Counter
class Solution:
def minimumPushes(self, word: str) -> int:
freq = Counter(word)
sorted_counts = sorted(freq.values(), reverse=True)
total_pushes = 0
for i, count in enumerate(sorted_counts):
push_count = (i // 8) + 1
total_pushes += push_count * count
return total_pushes
/**
* Problem: LeetCode 3016 - Minimum Number of Pushes to Type Word II
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
/**
* Problem: LeetCode 3016 - Minimum Number of Pushes to Type Word II
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int minimumPushes(string word) {
vector<int> freq(26, 0);
for (char c : word) {
freq[c - 'a']++;
}
sort(freq.rbegin(), freq.rend());
int totalPushes = 0;
for (int i = 0; i < 26; i++) {
if (freq[i] == 0) break;
int pushCount = (i / 8) + 1;
totalPushes += pushCount * freq[i];
}
return totalPushes;
}
};
/**
* Problem: LeetCode 3016 - Minimum Number of Pushes to Type Word II
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var minimumPushes = function(word) {
const freq = new Array(26).fill(0);
const codeA = 'a'.charCodeAt(0);
for (let i = 0; i < word.length; i++) {
freq[word.charCodeAt(i) - codeA]++;
}
freq.sort((a, b) => b - a);
let totalPushes = 0;
for (let i = 0; i < 26; i++) {
if (freq[i] === 0) break;
const pushCount = Math.floor(i / 8) + 1;
totalPushes += pushCount * freq[i];
}
return totalPushes;
};