-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode1629.js
29 lines (25 loc) · 944 Bytes
/
leetcode1629.js
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
/**
* @param {number[]} releaseTimes
* @param {string} keysPressed
* @return {character}
* https://leetcode.com/problems/slowest-key/
* https://leetcode.com/submissions/detail/550376218/
*/
var slowestKey = function (releaseTimes, keysPressed) {
let maxDuration = 0;
let maxLetter = "1"; //cheese with the fact that any letter is greater than string version of numeric 1;
let last = 0;
for (let i = 0; i < releaseTimes.length; i++) {
//new duration longer than previous duration, update maxDuration and maxLetter
if (releaseTimes[i] - last > maxDuration) {
maxDuration = releaseTimes[i] - last;
maxLetter = keysPressed[i];
}
//new duration same as maxDuration, do lexi comparison of maxLetter and current letter
if (releaseTimes[i] - last === maxDuration) {
maxLetter = keysPressed[i] > maxLetter ? keysPressed[i] : maxLetter;
}
last = releaseTimes[i];
}
return maxLetter;
};