mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 18:07:32 +01:00
34 lines
805 B
C#
34 lines
805 B
C#
|
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;
|
||
|
}
|
||
|
}
|