g1601_1700.s1629_slowest_key.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g1601_1700.s1629_slowest_key;
// #Easy #Array #String #2022_04_18_Time_4_ms_(14.60%)_Space_42.9_MB_(72.62%)
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Solution {
public char slowestKey(int[] releaseTimes, String keysPressed) {
Map 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> map2 = new HashMap<>();
for (Map.Entry entry : map.entrySet()) {
int duration = entry.getValue();
if (!map2.containsKey(duration)) {
map2.put(duration, new ArrayList<>());
}
map2.get(duration).add(entry.getKey());
}
int max = -1;
for (Map.Entry> entry : map2.entrySet()) {
List chars = entry.getValue();
Collections.sort(chars);
map2.put(entry.getKey(), chars);
max = Math.max(max, entry.getKey());
}
return map2.get(max).get(map2.get(max).size() - 1);
}
}