[LeetCode] Button with Longest Push Time

3386. Button with Longest Push Time

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] = [indexi, timei] indicates that the button at index indexi was pressed at time timei.

  • 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
func buttonWithLongestTime(events [][]int) int {
res := []int{0, 0}
p := 0
for _, e := range events {
btn, t := e[0], e[1]
d := t - p
p = t
if d > res[0] || (d == res[0] && -btn > res[1]) {
res = []int{d, -btn}
}
}
return -res[1]
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/12/15/PS/LeetCode/button-with-longest-push-time/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.