-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.js
More file actions
32 lines (28 loc) · 768 Bytes
/
Copy pathsolution.js
File metadata and controls
32 lines (28 loc) · 768 Bytes
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
/**
* @param {number[]} a
* @param {number[]} b
* @returns {number}
*/
class Solution {
LCIS(a, b) {
let n = a.length,
m = b.length;
if (n === 0 || m === 0) return 0;
// dp[j] = length of LCIS that ends with b[j]
const dp = new Array(m).fill(0);
for (let i = 0; i < n; ++i) {
let current = 0; // best LCIS length for values < a[i]
for (let j = 0; j < m; ++j) {
if (a[i] === b[j]) {
// extend sequences that end with smaller values
dp[j] = Math.max(dp[j], current + 1);
} else if (a[i] > b[j]) {
// candidate prefix
current = Math.max(current, dp[j]);
}
// else ignore
}
}
return Math.max(...dp);
}
}