Slowest Key
The problem is about finding the key with the longest duration pressed on a keyboard. We are given the times when keys were released and the sequence of keys pressed. The duration of each key press is the difference between the release time of the current key and the previous key.
Here’s a step-by-step solution:
- Initialize variables to track the longest duration and the corresponding key.
- Iterate through the
releaseTimes
and compute the duration of each key press. - If the current duration is greater than the longest duration found so far or is equal to the longest duration but the current key is lexicographically larger, update the longest duration and corresponding key.
- Return the key with the longest duration.
Here’s the code:
|
|
Example
For the input releaseTimes = [9,29,49,50], keysPressed = "cbcd"
, the method would return "c"
since the keypresses for ‘b’ and ‘c’ both have a duration of 20, but ‘c’ is lexicographically larger.