-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3014-MinimumNumberOfPushesToTypeWordI.go
More file actions
109 lines (95 loc) · 4.03 KB
/
3014-MinimumNumberOfPushesToTypeWordI.go
File metadata and controls
109 lines (95 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
// 3014. Minimum Number of Pushes to Type Word I
// You are given a string word containing distinct 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.
// 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.
// <img src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" />
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" />
// Input: word = "abcde"
// Output: 5
// Explanation: The remapped keypad given in the image provides the minimum cost.
// "a" -> one push on key 2
// "b" -> one push on key 3
// "c" -> one push on key 4
// "d" -> one push on key 5
// "e" -> one push on key 6
// Total cost is 1 + 1 + 1 + 1 + 1 = 5.
// It can be shown that no other mapping can provide a lower cost.
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" />
// Input: word = "xycdefghij"
// Output: 12
// Explanation: The remapped keypad given in the image provides the minimum cost.
// "x" -> one push on key 2
// "y" -> two pushes on key 2
// "c" -> one push on key 3
// "d" -> two pushes on key 3
// "e" -> one push on key 4
// "f" -> one push on key 5
// "g" -> one push on key 6
// "h" -> one push on key 7
// "i" -> one push on key 8
// "j" -> one push on key 9
// Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
// It can be shown that no other mapping can provide a lower cost.
// Constraints:
// 1 <= word.length <= 26
// word consists of lowercase English letters.
// All letters in word are distinct.
import "fmt"
func minimumPushes(word string) int {
res := 0
min := func (x, y int) int { if x < y { return x; }; return y; }
for n, i := len(word), 1; n > 0; n, i = n - 8, i + 1 {
res += min(8, n) * i
}
return res
}
func minimumPushes1(word string) int {
n := len(word)
max := func (x, y int) int { if x > y { return x; }; return y; }
return n + max(0, n - 8) + max(0, n - 16) + max(0, n - 24)
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" />
// Input: word = "abcde"
// Output: 5
// Explanation: The remapped keypad given in the image provides the minimum cost.
// "a" -> one push on key 2
// "b" -> one push on key 3
// "c" -> one push on key 4
// "d" -> one push on key 5
// "e" -> one push on key 6
// Total cost is 1 + 1 + 1 + 1 + 1 = 5.
// It can be shown that no other mapping can provide a lower cost.
fmt.Println(minimumPushes("abcde")) // 5
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" />
// Input: word = "xycdefghij"
// Output: 12
// Explanation: The remapped keypad given in the image provides the minimum cost.
// "x" -> one push on key 2
// "y" -> two pushes on key 2
// "c" -> one push on key 3
// "d" -> two pushes on key 3
// "e" -> one push on key 4
// "f" -> one push on key 5
// "g" -> one push on key 6
// "h" -> one push on key 7
// "i" -> one push on key 8
// "j" -> one push on key 9
// Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
// It can be shown that no other mapping can provide a lower cost.
fmt.Println(minimumPushes("xycdefghij")) // 12
fmt.Println(minimumPushes1("abcde")) // 5
fmt.Println(minimumPushes1("xycdefghij")) // 12
}