-
Notifications
You must be signed in to change notification settings - Fork 0
/
lis.js
34 lines (29 loc) · 795 Bytes
/
lis.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
const S = [4, 7, 6, 9, 8, 10]
const n = S.length
const choices = new Array(S.length + 1)
const cache = new Array(S.length + 1).fill(-1)
const lis = (start) => {
let result = cache[start + 1]
if (result !== -1) return result
cache[start + 1] = 1
let bestNext = -1
for (let next = start + 1; next < n; next ++) {
if (start === -1 || S[start] < S[next]) {
const cand = lis(next) + 1
if (cand > cache[start + 1]) {
cache[start + 1] = cand
bestNext = next
}
}
}
choices[start + 1] = bestNext
return cache[start + 1]
}
console.log(lis(0))
const reconstruct = (start, seq) => {
if (start !== -1) seq.push(S[start])
const next = choices[start + 1]
if (next !== -1) reconstruct(next, seq)
return seq
}
console.log(reconstruct(0, []))