-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestIncreasingSubsequence.js
More file actions
38 lines (35 loc) · 991 Bytes
/
Copy pathlongestIncreasingSubsequence.js
File metadata and controls
38 lines (35 loc) · 991 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
33
34
35
36
37
38
/**
* @param {number[]} nums
* @return {number}
*/
// 10, 15, 20, 25, 30, 40 -> index = 2
// 10, 15, 20, 20, 30, 40 -> index = 2
// 10, 15, 22, 25, 30, 40 -> index = 2
function lower_bound(arr, start, end, target) {
while(start < end){
let mid = Math.floor((start + end)/2);
if(target == arr[mid]) {
end = mid;
} else if (target < arr[mid]) {
end = mid;
} else {
start = mid + 1;
}
}
while(start < arr.length && arr[start] < target) {
start++;
}
return start;
}
var lengthOfLIS = function(nums) {
let lisArr = [];
for(let itr = 0; itr < nums.length; itr++) {
if(itr == 0 || lisArr[lisArr.length-1] < nums[itr] ) {
lisArr.push(nums[itr]);
} else {
let lowerBoundIndex = lower_bound(lisArr, 0, lisArr.length, nums[itr]);
lisArr[lowerBoundIndex] = nums[itr];
}
}
return lisArr.length;
};