forked from pratyushmp/code_opensource_2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrank_SlowestKeyPress.java
More file actions
58 lines (42 loc) · 1.53 KB
/
Hackerrank_SlowestKeyPress.java
File metadata and controls
58 lines (42 loc) · 1.53 KB
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
49
50
51
52
53
54
55
56
57
58
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
// Slowest Key press
public class Hackerrank_SlowestKeyPress {
public static void main(String[] args) {
//[[0, 2], [1, 5], [0, 9], [2, 15]];
Integer[][] keyTimes = new Integer[][]{{0, 2}, {1, 5}, {0, 9}, {2, 15}};
List<List<Integer>> listKeyTimes = new LinkedList<>();
for (int i = 0; i < keyTimes.length; i++) {
Integer[] keyTime = keyTimes[i];
List<Integer> ints = Arrays.asList(keyTime);
listKeyTimes.add(ints);
}
char ch = slowestKey(listKeyTimes);
System.out.printf("Slowest key: %s\n", ch);
}
public static char slowestKey(List<List<Integer>> keyTimes) {
int longestPress = 0;
int longestPressKey = 0;
for (int i = 0; i < keyTimes.size() - 1; i++) {
int key = keyTimes.get(i).get(0);
int countPressed = keyTimes.get(i).get(1);
if (i == 0) {
longestPressKey = key;
longestPress = countPressed;
}
int key2 = keyTimes.get(i + 1).get(0);
int countPressed2 = keyTimes.get(i + 1).get(1);
int diff = countPressed2 - countPressed;
if (longestPress < diff) {
longestPress = diff;
longestPressKey = key2;
}
}
char ch = keyToChar(longestPressKey);
return ch;
}
private static char keyToChar(int key) {
return (char) ((char) key + 'a');
}
}