Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b972176

Browse files
committedMar 16, 2020
提交300 最长上升子序列
1 parent 1469dab commit b972176

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
 
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<script>
10+
// Source : https://leetcode.com/problems/longest-increasing-subsequence/
11+
// Author : 悬笔e绝
12+
// Date : 2020-03-16
13+
14+
/**
15+
* @param {number[]} nums
16+
* @return {number}
17+
*/
18+
19+
function binarySearch(a, target) {
20+
var start = 0
21+
, end = a.length - 1;
22+
while(start <= end) {
23+
var mid = ~~((start + end) >> 1);
24+
if (a[mid] >= target)
25+
end = mid - 1;
26+
else
27+
start = mid + 1;
28+
}
29+
return end;
30+
}
31+
32+
var lengthOfLIS = function(nums) {
33+
var a = [];
34+
nums.forEach(function(item) {
35+
var index = binarySearch(a, item) + 1;
36+
if (a[index] === undefined)
37+
a[index] = item;
38+
else
39+
a[index] = Math.min(a[index], item);
40+
});
41+
return a.length;
42+
};
43+
44+
// 测试
45+
const nums = [10,9,2,5,3,7,101,18]
46+
console.log(lengthOfLIS(nums));
47+
48+
</script>
49+
</body>
50+
</html>

0 commit comments

Comments
 (0)