Longest Increasing Subsequence
lc 300
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.
dp, 最长上升序列,当i比j小或者想同时,这里最长只能是1了,只有i比j大,才能更新
public class Solution {
public int lengthOfLIS(int[] nums) {
if (nums == null || nums.length == 0 ) return 0;
int res = 1;
int[] f = new int[nums.length];
f[0] = 1;
for (int i = 1 ; i < nums.length; i++){
for ( int j = 0; j < i; j++){
if(nums[i] <= nums[j]){
f[i] = Math.max(1, f[i]);
continue;
}else{
f[i] =Math.max(f[i], f[j] + 1);
res = Math.max(res, f[i]);
}
}
}
return res;
}
}