1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-14 18:07:32 +01:00
LeetCode/cs/minimum-number-of-pushes-to-type-word-ii.cs

34 lines
805 B
C#
Raw Normal View History

public class Solution {
private int[] GetFreqs(string word) {
var freqs = new int[26];
foreach (char c in word) {
++freqs[c - 'a'];
}
return freqs;
}
public int MinimumPushes(string word) {
// Count occurences of letters
var freqs = GetFreqs(word);
// Sort them (letters don't matter, only counts)
Array.Sort(freqs);
var pushes = 0;
var (minPushes, used) = (1, 0);
for (var i = freqs.Length - 1; i >= 0 && freqs[i] > 0; --i) {
pushes += freqs[i] * minPushes;
++used;
// used all buttons for the current position
if (used >= 8) {
++minPushes;
used = 0;
}
}
return pushes;
}
}