We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a48ad19 commit 2190278Copy full SHA for 2190278
longest-increasing-subsequence/HC-kang.ts
@@ -0,0 +1,32 @@
1
+/**
2
+ * https://leetcode.com/problems/longest-increasing-subsequence
3
+ * T.C. O(nlogn)
4
+ * S.C. O(n)
5
+ */
6
+function lengthOfLIS(nums: number[]): number {
7
+ const sub: number[] = [];
8
+
9
+ function findSlot(num: number): number {
10
+ let left = 0;
11
+ let right = sub.length - 1;
12
13
+ while (left <= right) {
14
+ const mid = Math.floor((left + right) / 2);
15
+ if (sub[mid] < num) {
16
+ left = mid + 1;
17
+ } else {
18
+ right = mid - 1;
19
+ }
20
21
22
+ return left;
23
24
25
+ for (let i = 0; i < nums.length; i++) {
26
+ const num = nums[i];
27
+ const slot = findSlot(num);
28
+ sub[slot] = num;
29
30
31
+ return sub.length;
32
+}
0 commit comments