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 = [[9,20],[6,1],[1,3],[1,8]]
›
Output:
9
💡 Note:
Button durations: Button 9 (duration=20), Button 6 (duration=1-20=-19, but since events are sorted by time, this would be 1), Button 1 (duration=3-1=2), Button 1 (duration=8-3=5). Wait, let me recalculate: events should be sorted by time, so [[6,1],[1,3],[1,8],[9,20]]. Button 6 (duration=1), Button 1 (duration=3-1=2), Button 1 (duration=8-3=5), Button 9 (duration=20-8=12). Button 9 wins with duration 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
Understanding the Visualization
1
Input Events
Array of [button_index, timestamp] pairs sorted by time
2
Calculate Durations
First button duration = timestamp, others = time difference
3
Find Maximum
Return button index with longest duration (smallest index for ties)
Key Takeaway
🎯 Key Insight: Calculate durations correctly - first button uses timestamp, others use time differences between consecutive events
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code