Skip to content

Commit 3607b3b

Browse files
committed
1974. Minimum Time to Type Word Using Special Typewriter: AC
1 parent ac7f26e commit 3607b3b

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1487,3 +1487,4 @@ mod s1968_array_with_elements_not_equal_to_average_of_neighbors;
14871487
mod s1969_minimum_non_zero_product_of_the_array_elements;
14881488
mod s1970_last_day_where_you_can_still_cross;
14891489
mod s1971_find_if_path_exists_in_graph;
1490+
mod s1974_minimum_time_to_type_word_using_special_typewriter;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* [1974] Minimum Time to Type Word Using Special Typewriter
3+
*
4+
* 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'.
5+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/07/31/chart.jpg" style="width: 530px; height: 410px;" />
6+
* Each second, you may perform one of the following operations:
7+
*
8+
* Move the pointer one character counterclockwise or clockwise.
9+
* Type the character the pointer is currently on.
10+
*
11+
* Given a string word, return the minimum number of seconds to type out the characters in word.
12+
*
13+
* Example 1:
14+
*
15+
* Input: word = "abc"
16+
* Output: 5
17+
* Explanation:
18+
* The characters are printed as follows:
19+
* - Type the character 'a' in 1 second since the pointer is initially on 'a'.
20+
* - Move the pointer clockwise to 'b' in 1 second.
21+
* - Type the character 'b' in 1 second.
22+
* - Move the pointer clockwise to 'c' in 1 second.
23+
* - Type the character 'c' in 1 second.
24+
*
25+
* Example 2:
26+
*
27+
* Input: word = "bza"
28+
* Output: 7
29+
* Explanation:
30+
* The characters are printed as follows:
31+
* - Move the pointer clockwise to 'b' in 1 second.
32+
* - Type the character 'b' in 1 second.
33+
* - Move the pointer counterclockwise to 'z' in 2 seconds.
34+
* - Type the character 'z' in 1 second.
35+
* - Move the pointer clockwise to 'a' in 1 second.
36+
* - Type the character 'a' in 1 second.
37+
*
38+
* Example 3:
39+
*
40+
* Input: word = "zjpc"
41+
* Output: 34
42+
* Explanation:
43+
* The characters are printed as follows:
44+
* - Move the pointer counterclockwise to 'z' in 1 second.
45+
* - Type the character 'z' in 1 second.
46+
* - Move the pointer clockwise to 'j' in 10 seconds.
47+
* - Type the character 'j' in 1 second.
48+
* - Move the pointer clockwise to 'p' in 6 seconds.
49+
* - Type the character 'p' in 1 second.
50+
* - Move the pointer counterclockwise to 'c' in 13 seconds.
51+
* - Type the character 'c' in 1 second.
52+
*
53+
*
54+
* Constraints:
55+
*
56+
* 1 <= word.length <= 100
57+
* word consists of lowercase English letters.
58+
*
59+
*/
60+
pub struct Solution {}
61+
62+
// problem: https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/
63+
// discuss: https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/discuss/?currentPage=1&orderBy=most_votes&query=
64+
65+
// submission codes start here
66+
67+
impl Solution {
68+
pub fn min_time_to_type(word: String) -> i32 {
69+
word.bytes()
70+
.fold((word.len() as i32, b'a'), |(time, pointer), b| {
71+
if pointer > b {
72+
(time + (pointer - b).min(26 + b - pointer) as i32, b)
73+
} else {
74+
(time + (b - pointer).min(26 + pointer - b) as i32, b)
75+
}
76+
})
77+
.0
78+
}
79+
}
80+
81+
// submission codes end
82+
83+
#[cfg(test)]
84+
mod tests {
85+
use super::*;
86+
87+
#[test]
88+
fn test_1974_example_1() {
89+
let word = "abc".to_string();
90+
91+
let result = 5;
92+
93+
assert_eq!(Solution::min_time_to_type(word), result);
94+
}
95+
96+
#[test]
97+
fn test_1974_example_2() {
98+
let word = "bza".to_string();
99+
100+
let result = 7;
101+
102+
assert_eq!(Solution::min_time_to_type(word), result);
103+
}
104+
105+
#[test]
106+
fn test_1974_example_3() {
107+
let word = "zjpc".to_string();
108+
109+
let result = 34;
110+
111+
assert_eq!(Solution::min_time_to_type(word), result);
112+
}
113+
}

0 commit comments

Comments
 (0)