-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
46 lines (38 loc) · 762 Bytes
/
res.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}
*/
var lengthOfLIS = function(nums) {
const len = nums.length;
if (len < 2) return len;
const res = nums.map(() => 1);
for (let i = 0; i < len; i++) {
for (let j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
res[i] = Math.max(res[i], res[j]+1);
}
}
}
let max = res[0];
res.map(e => e > max ? max=e:null);
return max;
};
const lengthOfLIS_2 = (nums) => {
let tails = nums.map(() => 0), res = 0;
nums.map((num) => {
let i = 0, j = res;
while (i<j) {
const m = (i+j) >> 1;
if (tails[m] < num) {
i = m+1;
} else {
j = m;
}
}
tails[i] = num;
if (j === res) {
res += 1;
}
});
return res;
}