-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3386.java
32 lines (30 loc) · 1.12 KB
/
_3386.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.fishercoder.solutions.fourththousand;
import java.util.HashMap;
import java.util.Map;
public class _3386 {
public static class Solution1 {
public int buttonWithLongestTime(int[][] events) {
Map<Integer, Integer> map = new HashMap<>();
int ans = events[0][0];
map.put(events[0][0], events[0][1]);
for (int i = 1; i < events.length; i++) {
int duration = events[i][1] - events[i - 1][1];
if (map.getOrDefault(events[i][0], 0) < duration) {
map.put(events[i][0], duration);
}
}
int maxDuration = events[0][1];
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > maxDuration) {
ans = entry.getKey();
maxDuration = entry.getValue();
} else if (entry.getValue() == maxDuration) {
if (entry.getKey() < ans) {
ans = entry.getKey();
}
}
}
return ans;
}
}
}