|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +public class _1629 { |
| 10 | + public static class Solution1 { |
| 11 | + public char slowestKey(int[] releaseTimes, String keysPressed) { |
| 12 | + Map<Character, Integer> map = new HashMap<>(); |
| 13 | + for (int i = 0; i < releaseTimes.length; i++) { |
| 14 | + char c = keysPressed.charAt(i); |
| 15 | + int duration; |
| 16 | + if (i == 0) { |
| 17 | + duration = releaseTimes[i]; |
| 18 | + } else { |
| 19 | + duration = releaseTimes[i] - releaseTimes[i - 1]; |
| 20 | + } |
| 21 | + if (!map.containsKey(c)) { |
| 22 | + map.put(c, duration); |
| 23 | + } else { |
| 24 | + int val = map.get(c); |
| 25 | + if (duration > val) { |
| 26 | + map.put(c, duration); |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + Map<Integer, List<Character>> map2 = new HashMap<>(); |
| 31 | + for (char c : map.keySet()) { |
| 32 | + int duration = map.get(c); |
| 33 | + if (!map2.containsKey(duration)) { |
| 34 | + map2.put(duration, new ArrayList<>()); |
| 35 | + } |
| 36 | + map2.get(duration).add(c); |
| 37 | + } |
| 38 | + int max = -1; |
| 39 | + for (int duration : map2.keySet()) { |
| 40 | + List<Character> chars = map2.get(duration); |
| 41 | + Collections.sort(chars); |
| 42 | + map2.put(duration, chars); |
| 43 | + max = Math.max(max, duration); |
| 44 | + } |
| 45 | + return map2.get(max).get(map2.get(max).size() - 1); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments