Skip to content

Commit cb1bf4b

Browse files
committed
feat: add more
1 parent 583235d commit cb1bf4b

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed
Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// https://leetcode.com/problems/slowest-key
2-
//
2+
//
33
// A newly designed keypad was tested, where a tester pressed a sequence of `n` keys, one at a time.
4-
//
4+
//
55
// 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+
//
77
// 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+
//
99
// 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+
//
1111
// _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+
//
1313
// **Example 1:**
14-
//
14+
//
1515
// ```
1616
// **Input:** releaseTimes = [9,29,49,50], keysPressed = "cbcd"
1717
// **Output:** "c"
@@ -23,9 +23,9 @@
2323
// The longest of these was the keypress for 'b' and the second keypress for 'c', both with duration 20.
2424
// 'c' is lexicographically larger than 'b', so the answer is 'c'.
2525
// ```
26-
//
26+
//
2727
// **Example 2:**
28-
//
28+
//
2929
// ```
3030
// **Input:** releaseTimes = [12,23,36,46,62], keysPressed = "spuda"
3131
// **Output:** "a"
@@ -36,9 +36,9 @@
3636
// Keypress for 'd' had a duration of 46 - 36 = 10.
3737
// Keypress for 'a' had a duration of 62 - 46 = 16.
3838
// The longest of these was the keypress for 'a' with duration 16.```
39-
//
39+
//
4040
// **Constraints:**
41-
//
41+
//
4242
// * `releaseTimes.length == n`
4343
// * `keysPressed.length == n`
4444
// * `2 <= n <= 1000`
@@ -47,9 +47,24 @@
4747
// * `keysPressed` contains only lowercase English letters.
4848

4949
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+
}
5159
}
60+
return max_char;
61+
}
5262

5363
#[test]
5464
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+
);
5570
}

Array/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ mod _1572_matrix_diagonal_sum;
106106
mod _1582_special_positions_in_a_binary_matrix;
107107
mod _1588_sum_of_all_odd_length_subarrays;
108108
mod _1619_mean_of_array_after_removing_some_elements;
109+
mod _1629_slowest_key;
109110
mod _1636_sort_array_by_increasing_frequency;
110111
mod _1640_check_array_formation_through_concatenation;
111112
mod _1652_defuse_the_bomb;

0 commit comments

Comments
 (0)