-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlowestKeyPress.java
More file actions
23 lines (20 loc) · 813 Bytes
/
Copy pathSlowestKeyPress.java
File metadata and controls
23 lines (20 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package LeetcodePractice;
public class SlowestKeyPress {
public static char slowestKey(int[] releaseTimes, String keysPressed) {
int n = releaseTimes.length;
int maxDuration = releaseTimes[0];
char result = keysPressed.charAt(0);
for (int i = 1; i < n; i++) {
int duration = releaseTimes[i] - releaseTimes[i - 1];
if (duration > maxDuration || (duration == maxDuration && keysPressed.charAt(i) > result)) {
maxDuration = duration;
result = keysPressed.charAt(i);
}
}
return result;
}
public static void main(String[] args) {
System.out.println(slowestKey(new int[]{9,29,49,50}, "cbcd"));
System.out.println(slowestKey(new int[]{12,23,36,46,62}, "spuda"));
}
}