Skip to content

Commit 0f3914b

Browse files
committed
feat: solve No.3016
1 parent 3fa6804 commit 0f3914b

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# 3016. Minimum Number of Pushes to Type Word II
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Hash Table, String, Greedy, Sorting, Counting.
5+
- Similar Questions: Letter Combinations of a Phone Number.
6+
7+
## Problem
8+
9+
You are given a string `word` containing lowercase English letters.
10+
11+
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"` **.**
12+
13+
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`.
14+
15+
Return **the **minimum** number of pushes needed to type **`word` **after remapping the keys**.
16+
17+
An example mapping of letters to keys on a telephone keypad is given below. Note that `1`, `*`, `#`, and `0` do **not** map to any letters.
18+
19+
![](https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png)
20+
21+
 
22+
Example 1:
23+
24+
![](https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png)
25+
26+
```
27+
Input: word = "abcde"
28+
Output: 5
29+
Explanation: The remapped keypad given in the image provides the minimum cost.
30+
"a" -> one push on key 2
31+
"b" -> one push on key 3
32+
"c" -> one push on key 4
33+
"d" -> one push on key 5
34+
"e" -> one push on key 6
35+
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
36+
It can be shown that no other mapping can provide a lower cost.
37+
```
38+
39+
Example 2:
40+
41+
![](https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png)
42+
43+
```
44+
Input: word = "xyzxyzxyzxyz"
45+
Output: 12
46+
Explanation: The remapped keypad given in the image provides the minimum cost.
47+
"x" -> one push on key 2
48+
"y" -> one push on key 3
49+
"z" -> one push on key 4
50+
Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12
51+
It can be shown that no other mapping can provide a lower cost.
52+
Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters.
53+
```
54+
55+
Example 3:
56+
57+
![](https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png)
58+
59+
```
60+
Input: word = "aabbccddeeffgghhiiiiii"
61+
Output: 24
62+
Explanation: The remapped keypad given in the image provides the minimum cost.
63+
"a" -> one push on key 2
64+
"b" -> one push on key 3
65+
"c" -> one push on key 4
66+
"d" -> one push on key 5
67+
"e" -> one push on key 6
68+
"f" -> one push on key 7
69+
"g" -> one push on key 8
70+
"h" -> two pushes on key 9
71+
"i" -> one push on key 9
72+
Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24.
73+
It can be shown that no other mapping can provide a lower cost.
74+
```
75+
76+
 
77+
**Constraints:**
78+
79+
80+
81+
- `1 <= word.length <= 105`
82+
83+
- `word` consists of lowercase English letters.
84+
85+
86+
87+
## Solution
88+
89+
```javascript
90+
/**
91+
* @param {string} word
92+
* @return {number}
93+
*/
94+
var minimumPushes = function(word) {
95+
var frequencyMap = Array(26).fill(0);
96+
var a = 'a'.charCodeAt(0);
97+
for (var i = 0; i < word.length; i++) {
98+
frequencyMap[word[i].charCodeAt(0) - a] += 1;
99+
}
100+
var arr = frequencyMap.sort((a, b) => b - a);
101+
var res = 0;
102+
for (var i = 0; i < arr.length; i++) {
103+
res += Math.ceil((i + 1) / 8) * arr[i];
104+
}
105+
return res;
106+
};
107+
```
108+
109+
**Explain:**
110+
111+
nope.
112+
113+
**Complexity:**
114+
115+
* Time complexity : O(n).
116+
* Space complexity : O(1).

0 commit comments

Comments
 (0)