aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--leetcode.h1
-rw-r--r--min_num_push_word2.c45
-rw-r--r--min_num_push_word2.py33
3 files changed, 79 insertions, 0 deletions
diff --git a/leetcode.h b/leetcode.h
index fa54916..c881193 100644
--- a/leetcode.h
+++ b/leetcode.h
@@ -1,4 +1,5 @@
#include "lib/uthash/src/uthash.h"
+#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
diff --git a/min_num_push_word2.c b/min_num_push_word2.c
new file mode 100644
index 0000000..1b6d057
--- /dev/null
+++ b/min_num_push_word2.c
@@ -0,0 +1,45 @@
+// 3016. Minimum Number of Pushes to Type Word II
+#include "leetcode.h"
+
+/*
+ * you are given a string 'word' containing lowercase english letters. telephone
+ * keypads have keys mapped with distinct collections of lowercase english
+ * letters, which can be used to form words by pushing them. for example, the
+ * key 2 is mapped with [a, b, c], we need to push the key one time to type 'a',
+ * two times to type 'b', and three times to type 'c'. it is allowed to remap
+ * the keys numbered 2 to 9 to distinct collections of letters. the keys can be
+ * remapped to any amount of letters, but each letter must be mapped to exactly
+ * one key. you need to find the minimum number of times the keys will be pushed
+ * to type the string 'word'. return the minimum number of pushes needed to type
+ * 'word' after remapping the keys.
+ */
+
+int cmp(const int *const a, const int *const b) { return -(*a - *b); }
+
+int minimumPushes(char *word) {
+ int *lower_freq = (int *)calloc(26, sizeof(int));
+ for (int i = 0; word[i] != '\0'; i++) {
+ assert(word[i] >= 'a' && word[i] <= 'z');
+ lower_freq[word[i] - 'a']++;
+ }
+ qsort(lower_freq, 26, sizeof(int), &cmp);
+ int ans = 0;
+ for (int i = 1; 1; i++) {
+ const int base_idx = 8 * (i - 1);
+ for (int j = 0; j < 8; j++) {
+ const int curr_idx = base_idx + j;
+ if (!(curr_idx < 26 && lower_freq[curr_idx]))
+ return ans;
+ ans += i * lower_freq[curr_idx];
+ }
+ }
+ free(lower_freq);
+ return ans;
+}
+
+int main() {
+ char *w1 = "abcde", *w2 = "xyzxyzxyzxyz", *w3 = "aabbccddeeffgghhiiiiii";
+ printf("%d\n", minimumPushes(w1)); // expect: 5
+ printf("%d\n", minimumPushes(w2)); // expect: 12
+ printf("%d\n", minimumPushes(w3)); // expect: 24
+}
diff --git a/min_num_push_word2.py b/min_num_push_word2.py
new file mode 100644
index 0000000..010c9a1
--- /dev/null
+++ b/min_num_push_word2.py
@@ -0,0 +1,33 @@
+# 3016. Minimum Number of Pushes to Type Word II
+from collections import Counter
+
+"""
+you are given a string 'word' containing lowercase english letters. telephone
+keypads have keys mapped with distinct collections of lowercase english
+letters, which can be used to form words by pushing them. for example, the
+key 2 is mapped with [a, b, c], we need to push the key one time to type 'a',
+two times to type 'b', and three times to type 'c'. it is allowed to remap
+the keys numbered 2 to 9 to distinct collections of letters. the keys can be
+remapped to any amount of letters, but each letter must be mapped to exactly
+one key. you need to find the minimum number of times the keys will be pushed
+to type the string 'word'. return the minimum number of pushes needed to type
+'word' after remapping the keys.
+"""
+
+
+class Solution(object):
+ def minimumPushes(self, word):
+ """
+ :type word: str
+ :rtype: int
+ """
+ return sum(
+ (i // 8 + 1) * j for i, j in enumerate(sorted(Counter(word).values())[::-1])
+ )
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.minimumPushes(word="abcde"))
+ print(obj.minimumPushes(word="xyzxyzxyzxyz"))
+ print(obj.minimumPushes(word="aabbccddeeffgghhiiiiii"))