Skip to main content
Back to problems
#3386
Easy Algorithms

Button with longest push time

Array
41.0% acceptance
Feb 24, 2026
77
74
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn button_with_longest_time(events: Vec<Vec<i32>>) -> i32 {
    let mut best_idx = events[0][0];
    let mut best_time = events[0][1]; // first button time = time of press
    for i in 1..events.len() {
      let t = events[i][1] - events[i-1][1];
      let idx = events[i][0];
      if t > best_time || (t == best_time && idx < best_idx) {
        best_time = t;
        best_idx = idx;
      }
    }
    best_idx
  }
}