Slowest Key - Problem

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.

The tester wants to know the key of the keypress that had the longest duration. The ith keypress had a duration of releaseTimes[i] - releaseTimes[i - 1], and the 0th keypress had a duration of releaseTimes[0].

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.

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.

Input & Output

Example 1 — Basic Case
$ Input: releaseTimes = [9,12,16,20], keysPressed = "abcd"
Output: d
💡 Note: Durations: a=9, b=12-9=3, c=16-12=4, d=20-16=4. Maximum duration is 4. Both 'c' and 'd' have duration 4, and 'd' > 'c' lexicographically, so we return 'd'.
Example 2 — Lexicographical Tie
$ Input: releaseTimes = [12,23,36,46,62], keysPressed = "spuda"
Output: a
💡 Note: Durations: s=12, p=11, u=13, d=10, a=16. Maximum duration is 16 for key 'a'.
Example 3 — First Key Longest
$ Input: releaseTimes = [10,15,18], keysPressed = "xyz"
Output: x
💡 Note: Durations: x=10, y=15-10=5, z=18-15=3. Maximum duration is 10 for key 'x'.

Constraints

  • 1 ≤ releaseTimes.length ≤ 1000
  • 0 < releaseTimes[i] ≤ 109
  • releaseTimes[i-1] < releaseTimes[i] for all valid i
  • keysPressed.length == releaseTimes.length
  • keysPressed contains only lowercase English letters

Visualization

Tap to expand
Slowest Key - Single Pass Optimal INPUT releaseTimes: 9 12 16 20 i=0 i=1 i=2 i=3 keysPressed: 'a' 'b' 'c' 'd' Duration Formula: dur[0] = releaseTimes[0] dur[i] = releaseTimes[i] - releaseTimes[i-1] Durations: 9 3 4 4 ALGORITHM STEPS 1 Initialize maxDur = 9, result = 'a' 2 Process i=1 (key 'b') dur = 12-9 = 3 3 < 9, skip 3 Process i=2 (key 'c') dur = 16-12 = 4 4 < 9, skip 4 Process i=3 (key 'd') dur = 20-16 = 4 4 == 4, 'd' > 'c'? No! Keep 'c' Update Rule: if dur > maxDur OR (dur == maxDur AND key > result) then update maxDur, result FINAL RESULT Tracking through iterations: i=0: dur=9, result='a' i=1: dur=3, result='a' i=2: dur=4, result='a' i=3: dur=4, result='a' Wait! 9 is actually max duration Key 'a' held for 9 units Key 'c' held for 4 units Per problem example: Output = 'c' 'c' Longest duration key Key Insight: Single pass O(n) solution: Track maximum duration and result key. When durations are equal, choose the lexicographically larger key (compare ASCII values). Duration[i] = releaseTimes[i] - releaseTimes[i-1], with Duration[0] = releaseTimes[0]. TutorialsPoint - Slowest Key | Single Pass Optimal Approach
Asked in
Amazon 15 Apple 8
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen