public class Solution {
public int lengthOfLIS(int[] nums) {
int[] dp =new int[nums.length];
int max = 0;
for(int i=0,len = nums.length;i<len;i++){
dp[i]=1;
for(int j=0;j<i;j++){
if(nums[j]<nums[i])
dp[i] = Math.max(dp[j]+1,dp[i]);
}
max = Math.max(dp[i],max);
}
return max;
}
}
从第一个开始检查,注意,并不是指连续的序列。详见题目:https://leetcode.com/problems/longest-increasing-subsequence/#/description
参考文章:http://www.hawstein.com/posts/dp-novice-to-advanced.html
follow up
334. Increasing Triplet Subsequence##
public class Solution {
public boolean increasingTriplet(int[] nums) {
int min = Integer.MAX_VALUE,sec_min = Integer.MAX_VALUE;
for(int i:nums){
if(i<=min) min = i;
else if(i<=sec_min) sec_min = i;
else return true;
}
return false;
}
}
这题的关键是要维护一个最小值和次小值。