Difficulty: Easy | Acceptance: 59.60% | Paid: No Topics: Array, String
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, and a 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.
The duration of the ith keypress is releaseTimes[i] - releaseTimes[i - 1], and the 0th keypress has a duration of releaseTimes[0].
Note that the 0th key was pressed at time 0, so its duration is simply releaseTimes[0].
Return the key of the keypress that had the longest duration. If there are multiple such keypresses, return the lexicographically largest key.
- Examples
- Constraints
- Single Pass Iteration
- Pre-calculation & Comparison
Examples
Example 1:
Input: releaseTimes = [9,29,49,50], keysPressed = "cbcd"
Output: "c"
Explanation: The keypresses were as follows:
Keypress for 'c' had a duration of 9 (pressed at time 0 and released at time 9).
Keypress for 'b' had a duration of 29 - 9 = 20 (pressed at time 9 right after the release of the previous character and released at time 29).
Keypress for 'c' had a duration of 49 - 29 = 20 (pressed at time 29 right after the release of the previous character and released at time 49).
Keypress for 'd' had a duration of 50 - 49 = 1 (pressed at time 49 right after the release of the previous character and released at time 50).
The longest of these was 20, performed by keys 'b' and 'c'. The lexicographically largest key is 'c'.
Example 2:
Input: releaseTimes = [12,23,36,46,62], keysPressed = "spuda"
Output: "a"
Explanation: The keypresses were as follows:
Keypress for 's' had a duration of 12.
Keypress for 'p' had a duration of 23 - 12 = 11.
Keypress for 'u' had a duration of 36 - 23 = 13.
Keypress for 'd' had a duration of 46 - 36 = 10.
Keypress for 'a' had a duration of 62 - 46 = 16.
The longest of these was 16, performed by key 'a'.
Constraints
2 <= releaseTimes.length <= 1000
1 <= releaseTimes[i] <= 10⁹
releaseTimes and keysPressed have the same length.
releaseTimes is strictly increasing.
keysPressed consists of lowercase English letters.
Single Pass Iteration
Intuition We can iterate through the arrays once, calculating the duration of each keypress on the fly by comparing the current release time with the previous one. We keep track of the maximum duration found so far and the corresponding key, updating the result whenever we find a longer duration or a lexicographically larger key in case of a tie.
Steps
- Initialize
max_durationwithreleaseTimes[0]andresultwithkeysPressed[0]. - Iterate from index 1 to the end of the arrays.
- Calculate
current_durationasreleaseTimes[i] - releaseTimes[i-1]. - If
current_durationis greater thanmax_duration, updatemax_durationand setresultto the current key. - If
current_durationequalsmax_duration, check if the current key is lexicographically larger thanresult. If so, updateresult. - Return
resultafter the loop finishes.
class Solution:
def slowestKey(self, releaseTimes: list[int], keysPressed: str) -> str:
max_dur = releaseTimes[0]
res = keysPressed[0]
for i in range(1, len(releaseTimes)):
dur = releaseTimes[i] - releaseTimes[i-1]
if dur > max_dur or (dur == max_dur and keysPressed[i] > res):
max_dur = dur
res = keysPressed[i]
return resComplexity
- Time: O(n), where n is the length of the array. We traverse the array exactly once.
- Space: O(1), we only use a few variables for storage.
- Notes: This is the most optimal approach as it minimizes both time and space complexity.
Pre-calculation & Comparison
Intuition We can separate the logic into two distinct phases: first, calculate the duration for every single keypress and store them in a new array; second, iterate through this new array to find the maximum duration and the corresponding key. This approach is more verbose but clearly separates the calculation from the comparison logic.
Steps
- Create a new array
durationsof the same length asreleaseTimes. - Set
durations[0]toreleaseTimes[0]. - Iterate from index 1 to n-1, calculating
durations[i]asreleaseTimes[i] - releaseTimes[i-1]. - Initialize
max_durationto 0 andresultto an empty character. - Iterate through the
durationsarray. - If
durations[i]is greater thanmax_duration, updatemax_durationandresult. - If
durations[i]equalsmax_durationandkeysPressed[i]is lexicographically larger thanresult, updateresult. - Return
result.
class Solution:
def slowestKey(self, releaseTimes: list[int], keysPressed: str) -> str:
n = len(releaseTimes)
durations = [0] * n
durations[0] = releaseTimes[0]
for i in range(1, n):
durations[i] = releaseTimes[i] - releaseTimes[i-1]
max_dur = -1
res = ''
for i in range(n):
if durations[i] > max_dur or (durations[i] == max_dur and keysPressed[i] > res):
max_dur = durations[i]
res = keysPressed[i]
return resComplexity
- Time: O(n), where n is the length of the array. We iterate through the array twice, which simplifies to O(n).
- Space: O(n), to store the auxiliary
durationsarray. - Notes: This approach uses extra space compared to the single pass method, which is unnecessary for this problem.