-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path300.longest-increasing-subsequence.js
46 lines (40 loc) · 1.13 KB
/
300.longest-increasing-subsequence.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
/**
* @param {number[]} nums
* @return {number}
*/
function calculateCurrentDPPointMaxPrice(currentPosition, dp, nums) {
for (let j = 0; j < currentPosition; j++) {
if (nums[currentPosition] > nums[j]) {
// console.log("dp [i]", dp[i], i, j);
//offer a position for it to increase by one
dp[currentPosition] = Math.max(dp[currentPosition], 1 + dp[j]);
}
}
}
var lengthOfLIS = function (nums) {
if (!nums || nums.length == 0) {
return 0;
}
if (nums.length == 1) {
return 1;
}
/*
this is a bottom up dp, you can call it bottom up tabulation
in each element in dp, it record the the maximum increasing
subsequence
10 9 2 5 3 7 101 18
1 1 1 2 2 3 4 4
问题缩小为,到i 这一段的最长增序列的长度是多少-> very smart
*/
// create an array with size nums and fill the array with 1
let dp = new Array(nums.length).fill(1);
let result = 1;
//iterate all elements
for (let i = 1; i < nums.length; i++) {
calculateCurrentDPPointMaxPrice(i, dp, nums);
//get the max
result = Math.max(result, dp[i]);
}
// console.log({ result, dp });
return result;
};