forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
300.longest-increasing-subsequence.js
45 lines (41 loc) · 1.31 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
/*
* @lc app=leetcode id=300 lang=javascript
*
* [300] Longest Increasing Subsequence
*/
/**
* @param {number[]} nums
* @return {number}
*/
var lengthOfLIS = function(nums) {
// 时间复杂度O(n^2)
// if (nums.length === 0) return 0;
// const dp = Array(nums.length).fill(1);
// let max = 1;
// for (let i = 0; i < nums.length; i++) {
// for (let j = 0; j < i; j++) {
// if (nums[i] > nums[j]) {
// dp[i] = Math.max(dp[j] + 1, dp[i]);
// }
// max = Math.max(max, dp[i]);
// }
// }
// return max;
// [ 10, 9, 2, 5, 3, 7, 101, 18 ]
// [ 2, 3, 5, 7, 9, 10, 18, 101 ]
// 参考: https://leetcode.com/problems/longest-increasing-subsequence/discuss/74824/JavaPython-Binary-search-O(nlogn)-time-with-explanation
// const tails = [];
// for (let i = 0; i < nums.length; i++) {
// let left = 0;
// let right = tails.length;
// while (left < right) {
// const mid = left + (right - left) / 2; // 防止溢出
// if (tails[mid] < nums[i]) left = mid + 1;
// else right = mid;
// }
// // 说明nums[i] 比如tails中所有数字都大,我们直接push
// if (right === tails.length) tails.push(nums[i]);
// else tails[right] = nums[i]; // 否则我们修改tails[right]
// }
// return tails.length;
};