#1629
Easy Algorithms Slowest key
Array String
59.5% acceptance
Feb 25, 2026
796
113
A newly designed keypad was tested, where a tester pressed a sequence of n keys, one at a time.
You are given a string keysPressed of length n, where keysPressed[i] was the ith key pressed in the testing sequence, and a sorted list releaseTimes, where releaseTimes[i] was the time the ith key was released. Both arrays are 0-indexed.
The 0th key was pressed at the time 0, and every subsequent key was pressed at the exact time the previous key was released.
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn slowest_key(release_times: Vec<i32>, keys_pressed: String) -> char {
let keys: Vec<char> = keys_pressed.chars().collect();
let mut max_dur = release_times[0];
let mut ans = keys[0];
for i in 1..release_times.len() {
let dur = release_times[i] - release_times[i - 1];
if dur > max_dur || (dur == max_dur && keys[i] > ans) {
max_dur = dur;
ans = keys[i];
}
}
ans
}
}