Domain 1EasyCORE

Slowest Key

The problem involves determining which keypress on a keypad had the longest duration based on given release times and keys pressed. The input consists of two arrays: `releaseTimes` and `keysPressed`. The output should be the key with the longest duration; if there are ties, return the lexicographically largest key.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#Array

1629. Slowest Key

Model: qwen2.5-coder:14b
Topics: Array
FAANG asked: 1
Acceptance rate: 59.1%


Problem Breakdown

The problem involves determining which keypress on a keypad had the longest duration based on given release times and keys pressed. The input consists of two arrays: releaseTimes and keysPressed. The output should be the key with the longest duration; if there are ties, return the lexicographically largest key.

Inputs:

  • releaseTimes: An array where each element represents the time a key was released.
  • keysPressed: A string where each character represents a key pressed at corresponding times in releaseTimes.

Outputs:

  • A single character representing the key with the longest duration.

Implicit Constraints:

  • The first key is always pressed at time 0.
  • Subsequent keys are pressed immediately after the previous key is released.
  • The arrays are sorted and have the same length, n.
  • keysPressed contains only lowercase English letters.

Intuition

A brute-force approach would involve iterating through each keypress, calculating its duration, and keeping track of the maximum duration found. However, this approach might not be optimal in terms of time complexity if we need to handle larger inputs efficiently.

The key insight is to iterate through the releaseTimes array once, calculate the duration for each keypress, and maintain a record of the longest duration encountered so far. If two keys have the same duration, choose the lexicographically larger one.

Approach

  1. Initialize variables:

    • max_duration: To keep track of the maximum duration found.
    • slowest_key: To store the key with the longest duration.
  2. Iterate through the releaseTimes array:

    • For the first key (index 0), calculate its duration as releaseTimes[0].
    • For subsequent keys, calculate the duration as releaseTimes[i] - releaseTimes[i-1].
  3. Compare each calculated duration with max_duration:

    • If the current duration is greater than max_duration, update max_duration and set slowest_key to the current key.
    • If the current duration equals max_duration, update slowest_key if the current key is lexicographically larger.
  4. Return slowest_key.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the releaseTimes array. We iterate through the array once.
  • Space Complexity: O(1), as we use a constant amount of extra space for variables.

Solution

def slowestKey(releaseTimes, keysPressed):
    max_duration = releaseTimes[0]
    slowest_key = keysPressed[0]

    for i in range(1, len(releaseTimes)):
        duration = releaseTimes[i] - releaseTimes[i - 1]
        if duration > max_duration or (duration == max_duration and keysPressed[i] > slowest_key):
            max_duration = duration
            slowest_key = keysPressed[i]

    return slowest_key

Edge Cases Handled

  • Single Key Press: If there’s only one key press, the first key is returned as it has the entire release time as its duration.
  • All Keys Same Duration: If all keys have the same duration, the lexicographically largest key is returned.
  • Sequentially Increasing Times: The problem guarantees that releaseTimes is strictly increasing, so no need to handle decreasing or equal times.