|
1 | 1 | // https://leetcode.com/problems/slowest-key
|
2 |
| -// |
| 2 | +// |
3 | 3 | // A newly designed keypad was tested, where a tester pressed a sequence of `n` keys, one at a time.
|
4 |
| -// |
| 4 | +// |
5 | 5 | // You are given a string `keysPressed` of length `n`, where `keysPressed[i]` was the `i<sup>th</sup>` key pressed in the testing sequence, and a sorted list `releaseTimes`, where `releaseTimes[i]` was the time the `i<sup>th</sup>` key was released. Both arrays are **0-indexed**. The `0<sup>th</sup>` key was pressed at the time `0`, and every subsequent key was pressed at the **exact** time the previous key was released.
|
6 |
| -// |
| 6 | +// |
7 | 7 | // The tester wants to know the key of the keypress that had the **longest duration**. The `i<sup>th</sup>`keypress had a **duration** of `releaseTimes[i] - releaseTimes[i - 1]`, and the `0<sup>th</sup>` keypress had a duration of `releaseTimes[0]`.
|
8 |
| -// |
| 8 | +// |
9 | 9 | // Note that the same key could have been pressed multiple times during the test, and these multiple presses of the same key **may not** have had the same **duration**.
|
10 |
| -// |
| 10 | +// |
11 | 11 | // _Return the key of the keypress that had the **longest duration**. If there are multiple such keypresses, return the lexicographically largest key of the keypresses._
|
12 |
| -// |
| 12 | +// |
13 | 13 | // **Example 1:**
|
14 |
| -// |
| 14 | +// |
15 | 15 | // ```
|
16 | 16 | // **Input:** releaseTimes = [9,29,49,50], keysPressed = "cbcd"
|
17 | 17 | // **Output:** "c"
|
|
23 | 23 | // The longest of these was the keypress for 'b' and the second keypress for 'c', both with duration 20.
|
24 | 24 | // 'c' is lexicographically larger than 'b', so the answer is 'c'.
|
25 | 25 | // ```
|
26 |
| -// |
| 26 | +// |
27 | 27 | // **Example 2:**
|
28 |
| -// |
| 28 | +// |
29 | 29 | // ```
|
30 | 30 | // **Input:** releaseTimes = [12,23,36,46,62], keysPressed = "spuda"
|
31 | 31 | // **Output:** "a"
|
|
36 | 36 | // Keypress for 'd' had a duration of 46 - 36 = 10.
|
37 | 37 | // Keypress for 'a' had a duration of 62 - 46 = 16.
|
38 | 38 | // The longest of these was the keypress for 'a' with duration 16.```
|
39 |
| -// |
| 39 | +// |
40 | 40 | // **Constraints:**
|
41 |
| -// |
| 41 | +// |
42 | 42 | // * `releaseTimes.length == n`
|
43 | 43 | // * `keysPressed.length == n`
|
44 | 44 | // * `2 <= n <= 1000`
|
|
47 | 47 | // * `keysPressed` contains only lowercase English letters.
|
48 | 48 |
|
49 | 49 | pub fn slowest_key(release_times: Vec<i32>, keys_pressed: String) -> char {
|
50 |
| - |
| 50 | + let mut max_time = release_times[0]; |
| 51 | + let chars = keys_pressed.chars().collect::<Vec<char>>(); |
| 52 | + let mut max_char = chars[0]; |
| 53 | + for i in 1..chars.len() { |
| 54 | + let diff = (release_times[i] - release_times[i - 1]); |
| 55 | + if diff > max_time || (diff == max_time && chars[i] > max_char) { |
| 56 | + max_time = diff; |
| 57 | + max_char = chars[i]; |
| 58 | + } |
51 | 59 | }
|
| 60 | + return max_char; |
| 61 | +} |
52 | 62 |
|
53 | 63 | #[test]
|
54 | 64 | pub fn t1() {
|
| 65 | + assert_eq!(slowest_key(vec![9, 29, 49, 50], "cbcd".to_string()), 'c'); |
| 66 | + assert_eq!( |
| 67 | + slowest_key(vec![12, 23, 36, 46, 62], "spuda".to_string()), |
| 68 | + 'a' |
| 69 | + ); |
55 | 70 | }
|
0 commit comments