- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1629.java
48 lines (46 loc) · 1.65 KB
/
_1629.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
packagecom.fishercoder.solutions.secondthousand;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
publicclass_1629 {
publicstaticclassSolution1 {
publiccharslowestKey(int[] releaseTimes, StringkeysPressed) {
Map<Character, Integer> map = newHashMap<>();
for (inti = 0; i < releaseTimes.length; i++) {
charc = keysPressed.charAt(i);
intduration;
if (i == 0) {
duration = releaseTimes[i];
} else {
duration = releaseTimes[i] - releaseTimes[i - 1];
}
if (!map.containsKey(c)) {
map.put(c, duration);
} else {
intval = map.get(c);
if (duration > val) {
map.put(c, duration);
}
}
}
Map<Integer, List<Character>> map2 = newHashMap<>();
for (charc : map.keySet()) {
intduration = map.get(c);
if (!map2.containsKey(duration)) {
map2.put(duration, newArrayList<>());
}
map2.get(duration).add(c);
}
intmax = -1;
for (intduration : map2.keySet()) {
List<Character> chars = map2.get(duration);
Collections.sort(chars);
map2.put(duration, chars);
max = Math.max(max, duration);
}
returnmap2.get(max).get(map2.get(max).size() - 1);
}
}
}