File: slowest-key/solution.py

Date: 2026-06-06

Time: 19:08

slowest-key/solution.py

Purpose

This file solves LeetCode 1629: Slowest Key. It determines which key was held the longest based on an array of cumulative release times and a corresponding string of keys. It's one solution module in a repo of ~500+ LeetCode problems, each following the same solution.py / test_solution.py / review.md convention.

Key Components

minInteger(releaseTimes, keysPressed) -> str — The sole exported function. Despite the misleading name (minInteger suggests something numeric), it finds the key with the longest press duration, breaking ties by choosing the lexicographically largest key.

Contract:

Patterns

Single-pass greedy tracking. The algorithm maintains a running best (bestkey, bestdur) and updates in one linear scan — a standard pattern for "find the max with a secondary comparator" problems. No sorting, no auxiliary data structures.

Implicit first-element handling. The first keypress duration equals releaseTimes[0] (time since 0). Subsequent durations are computed as releaseTimes[i] - releaseTimes[i-1]. The code initializes best_dur = releaseTimes[0] to handle this asymmetry without a special case inside the loop.

Dependencies

Flow

1. Initialize bestkey to the first character and bestdur to releaseTimes[0] (duration of the first keypress, measured from time 0).

2. Iterate i from 1 to len(releaseTimes) - 1:

3. Return best_key.

Invariants

Error Handling

None. If releaseTimes or keysPressed is empty, line 13 (keysPressed[0]) raises an IndexError. This is consistent with the repo's convention of trusting LeetCode's input guarantees.

Topics to Explore

Beliefs