-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLongestIncreasingSubsequence.java
More file actions
35 lines (34 loc) · 1.04 KB
/
LongestIncreasingSubsequence.java
File metadata and controls
35 lines (34 loc) · 1.04 KB
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
public class Solution {
// O(n^2) / O(n)
public int lengthOfLIS(int[] nums) {
if (nums.length == 0) return 0;
int[] dp = new int[nums.length];
dp[0] = 1;
int res = 1;
for (int i = 1; i < nums.length; i++) {
dp[i] = 1;
for (int j = i - 1; j >= 0; j--) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[j] + 1, dp[i]);
res = Math.max(res, dp[i]);
}
}
}
return res;
}
// O(nlogn) / O(n)
public int lengthOfLIS(int[] nums) {
if (nums.length == 0) return 0;
List<Integer> seq = new ArrayList<>();
seq.add(nums[0]);
for (int i = 1; i < nums.length; i++) {
if (nums[i] > seq.get(seq.size() - 1)) {
seq.add(nums[i]);
} else {
int index = Collections.binarySearch(seq, nums[i]);
if (index < 0) seq.set(-(index + 1), nums[i]);
}
}
return seq.size();
}
}