Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given [10, 9, 2, 5, 3, 7, 101, 18]
,
The longest increasing subsequence is [2, 3, 7, 101]
, therefore the length is 4
. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
Solution 1 DP O(N ^ 2)//O(N ^ 2)
public static int lengthOfLIS(int[] nums) {
if(nums.length == 0 || nums == null){
return 0;
}
int []dp = new int[nums.length];
int max = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
if(dp[j] + 1 >= dp[i]){
dp[i] = dp[j] + 1;
}
}
}
if (dp[i] > max) {
max = dp[i];
}
}
return max;
}
Solution 2 Binary Search
Keep substituting the elements in the sequence, the result sequence might not be right.
//O(N LOG N)
public static int lengthOfLIS2(int[] nums) {
if (nums == null | nums.length == 0)
return 0;
int n = nums.length, len = 0;
int[] increasingSequence = new int[n];
increasingSequence[len++] = nums[0];
for (int i = 1; i < n; i++) {
if (nums[i] > increasingSequence[len - 1])
increasingSequence[len++] = nums[i];
else {
int position = findPositionToReplace(increasingSequence, 0, len - 1, nums[i]);
increasingSequence[position] = nums[i];
}
}
return len;
}
public static int findPositionToReplace(int[] a, int low, int high, int x) {
int mid;
while (low <= high) {
mid = low + (high - low) / 2;
if (a[mid] == x)
return mid;
else if (a[mid] > x)
high = mid - 1;
else
low = mid + 1;
}
return low;
}