cs: add «3016. Minimum Number of Pushes to Type Word II»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-06 16:00:02 +02:00
parent 989e7dfa33
commit edafb98470
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,33 @@
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;
}
}