LeetCode Problem Workspace

Button with Longest Push Time

Determine which button a child pressed the longest using an array-driven strategy to track time differences efficiently.

category

1

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Easy · Array-driven solution strategy

bolt

Answer-first summary

Determine which button a child pressed the longest using an array-driven strategy to track time differences efficiently.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array-driven solution strategy

Try AiBox Copilotarrow_forward

To solve Button with Longest Push Time, iterate through the events array, calculating each button's duration using time differences. Track the maximum duration and return the smallest index in case of ties. This approach leverages a simple array-driven strategy to efficiently capture both timing and ordering constraints in one pass.

Problem Statement

You are given a list of button events where each entry contains the button index and the time it was pressed. Your task is to identify which button was pressed for the longest duration based on consecutive time differences.

Return the index of the button with the maximum push duration. If multiple buttons have the same longest duration, return the smallest index among them. The input is sorted in increasing order of time.

Examples

Example 1

Input: events = [[1,2],[2,5],[3,9],[1,15]]

Output: 1

Example 2

Input: events = [[10,5],[1,7]]

Output: 10

Constraints

  • 1 <= events.length <= 1000
  • events[i] == [indexi, timei]
  • 1 <= indexi, timei <= 105
  • The input is generated such that events is sorted in increasing order of timei.

Solution Approach

Iterate and Calculate Durations

Loop through the events array, computing the difference between each time and the previous event to find the duration of each button press. Maintain a record of the longest duration seen so far and its corresponding button index.

Handle Ties with Minimum Index

When updating the longest duration, check if the current duration equals the maximum. If so, only update the result if the current button index is smaller than the stored one. This ensures the smallest index is returned on ties.

Return the Result

After processing all events, return the stored button index with the maximum push time. This approach runs in a single pass, using minimal extra space for tracking maximum duration and index.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(n) because each event is processed once. Space complexity is O(1) since only a few variables are needed for maximum tracking, regardless of input size.

What Interviewers Usually Probe

  • Candidate starts by calculating durations correctly from the events array.
  • Candidate correctly handles ties by comparing indices.
  • Candidate uses minimal space and a single-pass iteration showing array pattern understanding.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to compute duration using the previous event's time, not the first event only.
  • Not handling multiple buttons with equal maximum duration, ignoring smallest index rule.
  • Attempting to store all durations instead of tracking the maximum in a single pass, wasting space.

Follow-up variants

  • Input arrays with large numbers to test integer handling and duration calculation.
  • Events where the first and last buttons tie for maximum duration.
  • Sparse events with non-consecutive button indices to ensure array iteration logic is robust.

FAQ

What is the main strategy for Button with Longest Push Time?

Use an array-driven approach, iterating through events and calculating durations between consecutive presses.

How do I handle multiple buttons with the same longest push time?

Track the smallest index whenever a duration ties the current maximum.

Can this problem be solved in one pass?

Yes, by maintaining the current maximum duration and corresponding button index while iterating.

What is the expected time and space complexity?

Time complexity is O(n) and space complexity is O(1), as only a few variables are needed.

Does the order of events matter?

Yes, events are sorted by time and this order is crucial for correctly calculating durations.

terminal

Solution

Solution 1: Single Pass

We define two variables $\textit{ans}$ and $t$, representing the index of the button with the longest press time and the press time, respectively.

1
2
3
4
5
6
7
8
class Solution:
    def buttonWithLongestTime(self, events: List[List[int]]) -> int:
        ans, t = events[0]
        for (_, t1), (i, t2) in pairwise(events):
            d = t2 - t1
            if d > t or (d == t and i < ans):
                ans, t = i, d
        return ans
Button with Longest Push Time Solution: Array-driven solution strategy | LeetCode #3386 Easy