-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrank.js
48 lines (46 loc) · 1.31 KB
/
rank.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function maxTickets(tickets) {
// m = size of subsequence
// s = subsequence
// determine the max length of a subsequence in which the difference between any elements (in a sorted list) is either 0 or 1
// ex: tickets = [8, 5, 4, 8, 4]
// valid subsequences = [4, 4, 5] and [8, 8]
// m = 3 and 2
// 3 is the longest so 3 would be the returned value
let m = 0;
let sortedTickets = tickets.sort((a, b) => a - b);
let cache = Array(sortedTickets.length).fill(1);
let previous = 0;
let current = 1;
while (previous < sortedTickets.length) {
if (Math.abs(sortedTickets[previous] - sortedTickets[current]) <= 1) {
const newSubsequence = cache[previous] + 1;
if (newSubsequence > cache[current]) {
cache[current] = newSubsequence;
}
}
previous++;
if (previous === current) {
current++;
previous = 0;
}
}
m = cache.sort((a, b) => a - b);
return m[m.length - 1];
}
function toolchanger(tools, startIndex, target) {
let idx = startIndex;
let rightSide = 0,
leftSide = 0;
while (tools[idx] !== target) {
idx++;
rightSide++;
if (idx === tools.length) idx = 0;
}
idx = startIndex;
while (tools[idx] !== target) {
idx--;
leftSide++;
if (idx === -1) idx = tools.length - 1;
}
return Math.min(rightSide, leftSide);
}