Leetcode 300. Longest Increasing Subsequence

来源:互联网 发布:电脑软件常见故障排除 编辑:程序博客网 时间:2024/05/22 00:54
/** * let dp[i] be the length of the longest sub-array ending with nums[i] * then dp[n] = dp[m]+1 if nums[n] > nums[m] AND dp[m] is the largest (m is from 1 to n-1) *      else dp[n] = 1 */ public class Solution {    public int lengthOfLIS(int[] nums) {        if (nums.length == 0) return 0;        int max = 1;        int[] dp = new int[nums.length];        Arrays.fill(dp, 1);        for (int i=1; i<nums.length; i++) {            for (int j=0; j<i; j++)                 if (nums[i] > nums[j] && dp[i] <= dp[j])                    dp[i] = dp[j]+1;            if (max < dp[i]) max = dp[i];        }        return max;    }}

0 0