Skip to content

Commit 5eeaa46

Browse files
committed
Longest Increasing Subsequence
1 parent c19845f commit 5eeaa46

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+
// TC: O(n log n)
2+
// -> nums for loop O(n) + binarySearch O(log n)
3+
// SC: O(n)
4+
// -> ArrayList could have nums all elements
5+
class Solution {
6+
public int lengthOfLIS(int[] nums) {
7+
List<Integer> output = new ArrayList<>();
8+
9+
for (int num : nums) {
10+
int start = 0;
11+
int end = output.size();
12+
while (start < end) {
13+
int mid = start + (end - start) / 2;
14+
if (output.get(mid) < num) start = mid + 1;
15+
else end = mid;
16+
}
17+
if (start == output.size()) output.add(num);
18+
else output.set(start, num);
19+
}
20+
return output.size();
21+
}
22+
}

0 commit comments

Comments
 (0)