Skip to content

Commit 838abad

Browse files
committed
Added Longest Increasing Subsequence program to the list
1 parent e9fe0ce commit 838abad

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* A Dynamic Programming based solution for calculating Longest Increasing Subsequence
3+
* https://en.wikipedia.org/wiki/Longest_increasing_subsequence
4+
*/
5+
6+
function main () {
7+
const x = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]
8+
const length = x.length
9+
const dp = Array(length).fill(1)
10+
11+
let res = 1
12+
13+
for (let i = 0; i < length; i++) {
14+
for (let j = 0; j < i; j++) {
15+
if (x[i] > x[j]) {
16+
dp[i] = Math.max(dp[i], 1 + dp[j])
17+
if (dp[i] > res)
18+
res = dp[i]
19+
}
20+
}
21+
}
22+
23+
console.log('Length of Longest Increasing Subsequence is:', res)
24+
}
25+
26+
main()

0 commit comments

Comments
 (0)