From edafb984703553e76c300d8c59c31269424e0af7 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 6 Aug 2024 16:00:02 +0200 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB3016.=20Minimum=20Number=20of?= =?UTF-8?q?=20Pushes=20to=20Type=20Word=20II=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...inimum-number-of-pushes-to-type-word-ii.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 cs/minimum-number-of-pushes-to-type-word-ii.cs diff --git a/cs/minimum-number-of-pushes-to-type-word-ii.cs b/cs/minimum-number-of-pushes-to-type-word-ii.cs new file mode 100644 index 0000000..a6e8388 --- /dev/null +++ b/cs/minimum-number-of-pushes-to-type-word-ii.cs @@ -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; + } +}