-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1974-MinimumTimeToTypeWordUsingSpecialTypewriter.go
More file actions
124 lines (112 loc) · 4.36 KB
/
1974-MinimumTimeToTypeWordUsingSpecialTypewriter.go
File metadata and controls
124 lines (112 loc) · 4.36 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
// 1974. Minimum Time to Type Word Using Special Typewriter
// There is a special typewriter with lowercase English letters 'a' to 'z' arranged in a circle with a pointer.
// A character can only be typed if the pointer is pointing to that character.
// The pointer is initially pointing to the character 'a'.
// <img src="https://assets.leetcode.com/uploads/2021/07/31/chart.jpg" />
// Each second, you may perform one of the following operations:
// Move the pointer one character counterclockwise or clockwise.
// Type the character the pointer is currently on.
// Given a string word, return the minimum number of seconds to type out the characters in word.
// Example 1:
// Input: word = "abc"
// Output: 5
// Explanation:
// The characters are printed as follows:
// - Type the character 'a' in 1 second since the pointer is initially on 'a'.
// - Move the pointer clockwise to 'b' in 1 second.
// - Type the character 'b' in 1 second.
// - Move the pointer clockwise to 'c' in 1 second.
// - Type the character 'c' in 1 second.
// Example 2:
// Input: word = "bza"
// Output: 7
// Explanation:
// The characters are printed as follows:
// - Move the pointer clockwise to 'b' in 1 second.
// - Type the character 'b' in 1 second.
// - Move the pointer counterclockwise to 'z' in 2 seconds.
// - Type the character 'z' in 1 second.
// - Move the pointer clockwise to 'a' in 1 second.
// - Type the character 'a' in 1 second.
// Example 3:
// Input: word = "zjpc"
// Output: 34
// Explanation:
// The characters are printed as follows:
// - Move the pointer counterclockwise to 'z' in 1 second.
// - Type the character 'z' in 1 second.
// - Move the pointer clockwise to 'j' in 10 seconds.
// - Type the character 'j' in 1 second.
// - Move the pointer clockwise to 'p' in 6 seconds.
// - Type the character 'p' in 1 second.
// - Move the pointer counterclockwise to 'c' in 13 seconds.
// - Type the character 'c' in 1 second.
// Constraints:
// 1 <= word.length <= 100
// word consists of lowercase English letters.
import "fmt"
func minTimeToType(word string) int {
res, prev := 0, 'a'
abs := func(x int) int { if x < 0 { return -x; }; return x; }
min := func (x, y int) int { if x < y { return x; }; return y; }
for _, v := range word {
diff := abs(int(v) - int(prev))
res += min(diff, (26 - diff)) + 1
prev = v
}
return res
}
func minTimeToType1(word string) int {
res, cur := 0, 0
abs := func(x int) int { if x < 0 { return -x; }; return x; }
min := func (x, y int) int { if x < y { return x; }; return y; }
for _, v := range word {
c := int(v - 'a')
res += min(abs(c - cur), abs(abs(c - cur) - 26)) + 1
cur = c
}
return res
}
func main() {
// Example 1:
// Input: word = "abc"
// Output: 5
// Explanation:
// The characters are printed as follows:
// - Type the character 'a' in 1 second since the pointer is initially on 'a'.
// - Move the pointer clockwise to 'b' in 1 second.
// - Type the character 'b' in 1 second.
// - Move the pointer clockwise to 'c' in 1 second.
// - Type the character 'c' in 1 second.
fmt.Println(minTimeToType("abc")) // 5
// Example 2:
// Input: word = "bza"
// Output: 7
// Explanation:
// The characters are printed as follows:
// - Move the pointer clockwise to 'b' in 1 second.
// - Type the character 'b' in 1 second.
// - Move the pointer counterclockwise to 'z' in 2 seconds.
// - Type the character 'z' in 1 second.
// - Move the pointer clockwise to 'a' in 1 second.
// - Type the character 'a' in 1 second.
fmt.Println(minTimeToType("bza")) // 7
// Example 3:
// Input: word = "zjpc"
// Output: 34
// Explanation:
// The characters are printed as follows:
// - Move the pointer counterclockwise to 'z' in 1 second.
// - Type the character 'z' in 1 second.
// - Move the pointer clockwise to 'j' in 10 seconds.
// - Type the character 'j' in 1 second.
// - Move the pointer clockwise to 'p' in 6 seconds.
// - Type the character 'p' in 1 second.
// - Move the pointer counterclockwise to 'c' in 13 seconds.
// - Type the character 'c' in 1 second.
fmt.Println(minTimeToType("zjpc")) // 34
fmt.Println(minTimeToType1("abc")) // 5
fmt.Println(minTimeToType1("bza")) // 7
fmt.Println(minTimeToType1("zjpc")) // 34
}