-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlowestKey.java
23 lines (23 loc) · 947 Bytes
/
SlowestKey.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// https://leetcode.com/problems/slowest-key/
class Solution {
public char slowestKey(int[] releaseTimes, String keysPressed) {
TreeMap<Character, Integer> durationList = new TreeMap<>();
char result= ' ';
int longestDuration=0;
durationList.put(keysPressed.charAt(0),releaseTimes[0]); // n always 2<=n<=1000
for(int i=1; i<releaseTimes.length; i++){
int longest = releaseTimes[i]-releaseTimes[i-1];
char currentKey = keysPressed.charAt(i);
durationList.put(currentKey,Math.max(durationList.getOrDefault(currentKey, 0),longest));
}
for (var element : durationList.entrySet()) {
int duration = (int) element.getValue();
char key = (char) element.getKey();
if(duration >=longestDuration){
longestDuration = duration;
result = key;
}
}
return result;
}
}