Skip to content

Commit 003e11f

Browse files
committed
Add solution #3016
1 parent e88ca71 commit 003e11f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,7 @@
21312131
3011|[Find if Array Can Be Sorted](./solutions/3011-find-if-array-can-be-sorted.js)|Medium|
21322132
3014|[Minimum Number of Pushes to Type Word I](./solutions/3014-minimum-number-of-pushes-to-type-word-i.js)|Easy|
21332133
3015|[Count the Number of Houses at a Certain Distance I](./solutions/3015-count-the-number-of-houses-at-a-certain-distance-i.js)|Medium|
2134+
3016|[Minimum Number of Pushes to Type Word II](./solutions/3016-minimum-number-of-pushes-to-type-word-ii.js)|Medium|
21342135
3024|[Type of Triangle](./solutions/3024-type-of-triangle.js)|Easy|
21352136
3042|[Count Prefix and Suffix Pairs I](./solutions/3042-count-prefix-and-suffix-pairs-i.js)|Easy|
21362137
3066|[Minimum Operations to Exceed Threshold Value II](./solutions/3066-minimum-operations-to-exceed-threshold-value-ii.js)|Medium|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 3016. Minimum Number of Pushes to Type Word II
3+
* https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-ii/
4+
* Difficulty: Medium
5+
*
6+
* You are given a string word containing lowercase English letters.
7+
*
8+
* Telephone keypads have keys mapped with distinct collections of lowercase English letters,
9+
* which can be used to form words by pushing them. For example, the key 2 is mapped with
10+
* ["a","b","c"], we need to push the key one time to type "a", two times to type "b", and
11+
* three times to type "c".
12+
*
13+
* It is allowed to remap the keys numbered 2 to 9 to distinct collections of letters. The
14+
* keys can be remapped to any amount of letters, but each letter must be mapped to exactly
15+
* one key. You need to find the minimum number of times the keys will be pushed to type the
16+
* string word.
17+
*
18+
* Return the minimum number of pushes needed to type word after remapping the keys.
19+
*
20+
* An example mapping of letters to keys on a telephone keypad is given below. Note that
21+
* 1, *, #, and 0 do not map to any letters.
22+
*/
23+
24+
/**
25+
* @param {string} word
26+
* @return {number}
27+
*/
28+
var minimumPushes = function(word) {
29+
const frequency = new Array(26).fill(0);
30+
for (const char of word) {
31+
frequency[char.charCodeAt(0) - 97]++;
32+
}
33+
frequency.sort((a, b) => b - a);
34+
35+
let result = 0;
36+
for (let i = 0; i < frequency.length && frequency[i] > 0; i++) {
37+
result += frequency[i] * (Math.floor(i / 8) + 1);
38+
}
39+
40+
return result;
41+
};

0 commit comments

Comments
 (0)