Easy
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
.
time
.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
.
Example 1:
Input: events = [[1,2],[2,5],[3,9],[1,15]]
Output: 1
Explanation:
5 - 2 = 3
units of time.9 - 5 = 4
units of time.15 - 9 = 6
units of time.Example 2:
Input: events = [[10,5],[1,7]]
Output: 10
Explanation:
7 - 5 = 2
units of time.Constraints:
1 <= events.length <= 1000
events[i] == [indexi, timei]
1 <= indexi, timei <= 105
events
is sorted in increasing order of timei
.public class Solution {
public int buttonWithLongestTime(int[][] events) {
int ans = 0;
int time = 0;
int last = 0;
for (int[] event : events) {
int diff = event[1] - last;
if (diff > time) {
time = diff;
ans = event[0];
} else if (diff == time) {
ans = Math.min(ans, event[0]);
}
last = event[1];
}
return ans;
}
}