-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestIncreasingSubsequence.java
More file actions
33 lines (32 loc) · 1.01 KB
/
Copy pathLongestIncreasingSubsequence.java
File metadata and controls
33 lines (32 loc) · 1.01 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
public class LongestIncreasingSubsequence{
public int lengthOfLIS(int[] nums) {
int len = 0;
if(nums != null && nums.length != 0){
int[] lis = new int[nums.length];
lis[0] = 0;
len = 1;
for(int i = 1; i < nums.length; i++){
if(nums[i] < nums[lis[0]]){
lis[0] = i;
} else if(nums[i] > nums[lis[len - 1]]){
lis[len] = i;
len ++;
} else {
int next = findFirstLargerInclusive(nums, lis, 0, len, nums[i]);
lis[next] = i;
}
}
}
return len;
}
public int findFirstLargerInclusive(int[] nums, int[] lis, int start, int end, int target){
while(start < end){
int mid = start + (end - start) / 2;
if(nums[lis[mid]] < target)
start = mid + 1;
else
end = mid;
}
return start;
}
}