Skip to content

Commit 7997785

Browse files
authored
longest increasing subsequence solution
1 parent 9052e50 commit 7997785

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
int lengthOfLIS(List<int> nums) {
3+
final stack = []; // S(n) = O(n)
4+
nums.forEach((x) { // T(n) = O(nlgn)
5+
int i = -1, j = stack.length - 1;
6+
while (i != j) { // O(lgn)
7+
final int mid = j - ((j - i) >> 1);
8+
if (stack[mid] < x) {
9+
i = mid;
10+
} else {
11+
j = mid - 1;
12+
}
13+
}
14+
if (++i == stack.length) {
15+
stack.add(x);
16+
} else {
17+
stack[i] = x;
18+
}
19+
});
20+
return stack.length;
21+
}
22+
}

0 commit comments

Comments
 (0)