forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1629.java
48 lines (46 loc) · 1.64 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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _1629 {
public static class Solution1 {
public char slowestKey(int[] releaseTimes, String keysPressed) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < releaseTimes.length; i++) {
char c = keysPressed.charAt(i);
int duration;
if (i == 0) {
duration = releaseTimes[i];
} else {
duration = releaseTimes[i] - releaseTimes[i - 1];
}
if (!map.containsKey(c)) {
map.put(c, duration);
} else {
int val = map.get(c);
if (duration > val) {
map.put(c, duration);
}
}
}
Map<Integer, List<Character>> map2 = new HashMap<>();
for (char c : map.keySet()) {
int duration = map.get(c);
if (!map2.containsKey(duration)) {
map2.put(duration, new ArrayList<>());
}
map2.get(duration).add(c);
}
int max = -1;
for (int duration : map2.keySet()) {
List<Character> chars = map2.get(duration);
Collections.sort(chars);
map2.put(duration, chars);
max = Math.max(max, duration);
}
return map2.get(max).get(map2.get(max).size() - 1);
}
}
}