mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
cs: add «3016. Minimum Number of Pushes to Type Word II»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
989e7dfa33
commit
edafb98470
1 changed files with 33 additions and 0 deletions
33
cs/minimum-number-of-pushes-to-type-word-ii.cs
Normal file
33
cs/minimum-number-of-pushes-to-type-word-ii.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue