Skip to content

3386. Button with Longest Push Time

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
 public:
  int buttonWithLongestTime(vector<vector<int>>& events) {
    int ans = 0;
    int maxTimeTaken = 0;
    int prevTime = 0;

    for (const vector<int>& event : events) {
      const int index = event[0];
      const int time = event[1];
      const int timeTaken = time - prevTime;
      if (timeTaken > maxTimeTaken ||
          timeTaken == maxTimeTaken && index < ans) {
        maxTimeTaken = timeTaken;
        ans = index;
      }
      prevTime = time;
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
  public int buttonWithLongestTime(int[][] events) {
    int ans = 0;
    int maxTimeTaken = 0;
    int prevTime = 0;

    for (int[] event : events) {
      final int index = event[0];
      final int time = event[1];
      final int timeTaken = time - prevTime;
      if (timeTaken > maxTimeTaken || timeTaken == maxTimeTaken && index < ans) {
        maxTimeTaken = timeTaken;
        ans = index;
      }
      prevTime = time;
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution:
  def buttonWithLongestTime(self, events: list[list[int]]) -> int:
    ans = 0
    maxTimeTaken = 0
    prevTime = 0

    for index, time in events:
      timeTaken = time - prevTime
      if timeTaken > maxTimeTaken or timeTaken == maxTimeTaken and index < ans:
        maxTimeTaken = timeTaken
        ans = index
      prevTime = time

    return ans