Button with Longest Push Time - Problem

You are given a 2D array events which represents a sequence of events where a child pushes a series of buttons on a keyboard.

Each events[i] = [index_i, time_i] indicates that the button at index index_i was pressed at time time_i.

The array is sorted in increasing order of time. The time taken to press a button is the difference in time between consecutive button presses. The time for the first button is simply the time at which it was pressed.

Return the index of the button that took the longest time to push. If multiple buttons have the same longest time, return the button with the smallest index.

Input & Output

Example 1 — Basic Case
$ Input: events = [[1,2],[2,5],[3,9],[1,15]]
Output: 1
💡 Note: Button durations: Button 1 at time 2 (duration=2), Button 2 at time 5 (duration=5-2=3), Button 3 at time 9 (duration=9-5=4), Button 1 at time 15 (duration=15-9=6). Button 1 has the longest press duration of 6.
Example 2 — First Button Winner
$ Input: events = [[6,1],[1,3],[1,8],[9,20]]
Output: 9
💡 Note: Button durations: Button 6 (duration=1), Button 1 (duration=3-1=2), Button 1 (duration=8-3=5), Button 9 (duration=20-8=12). Button 9 has the longest press duration of 12.
Example 3 — Tie Breaking
$ Input: events = [[2,3],[1,7]]
Output: 1
💡 Note: Button durations: Button 2 (duration=3), Button 1 (duration=7-3=4). Button 1 has longer duration of 4, so return 1.

Constraints

  • 1 ≤ events.length ≤ 1000
  • events[i] = [indexi, timei]
  • 1 ≤ indexi, timei ≤ 105
  • events is sorted in increasing order of timei

Visualization

Tap to expand
Button with Longest Push Time INPUT events = [[1,2],[2,5],[3,9],[1,15]] Event 0: [1, 2] Button 1 at time 2 Event 1: [2, 5] Button 2 at time 5 Event 2: [3, 9] Button 3 at time 9 Event 3: [1, 15] Button 1 at time 15 Timeline t=2 t=5 t=9 t=15 B1 B2 B3 B1 ALGORITHM STEPS 1 Initialize maxTime=0, resultBtn=0 2 Process Event 0 pushTime = 2-0 = 2 maxTime=2, resultBtn=1 3 Process Event 1 pushTime = 5-2 = 3 maxTime=3, resultBtn=2 4 Process Event 2 pushTime = 9-5 = 4 maxTime=4, resultBtn=3 5 Process Event 3 pushTime = 15-9 = 6 maxTime=6, resultBtn=1 Push Time Summary B1:2 B2:3 B3:4 B1:6 Winner: Button 1 (time=6) FINAL RESULT Button with Longest Push Time 1 Button Index Output 1 Verification Button 1: max push = 6 Button 2: max push = 3 Button 3: max push = 4 Status: OK Key Insight: Single Pass Optimization: Process events in order, computing push time as current_time - previous_time. Track max push time and corresponding button. For ties, smaller index wins (early comparison). Time: O(n), Space: O(1) - Only need to track maxTime, resultButton, and prevTime variables. TutorialsPoint - Button with Longest Push Time | Optimized Single Pass with Early Comparison
Asked in
Meta 15 Amazon 12 Google 8
8.5K Views
Medium Frequency
~12 min Avg. Time
245 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