美文网首页
300. Longest Increasing Subseque

300. Longest Increasing Subseque

作者: becauseyou_90cd | 来源:发表于2018-07-23 22:27 被阅读0次

    https://leetcode.com/problems/longest-increasing-subsequence/description/

    解题思路:

    1. 两个循环来解决:计算当前值 num[i] 被包括在最大增长子序列的情况下dp[i][j]的最大值

    代码如下:
    class Solution {
    public int lengthOfLIS(int[] nums) {

        if(nums == null || nums.length == 0) return 0;
        int len = nums.length;
        int[] dp = new int[len];
        Arrays.fill(dp, 1);
        int max = 1;
        for(int i = 1; i < len; i++){
            for(int j = i - 1; j >= 0; j--){
                if(nums[i] > nums[j])
                    dp[i] = Math.max(dp[i], dp[j]  + 1);
            }
            max = Math.max(dp[i], max);
        }
        return max;
    }
    

    }

    相关文章

      网友评论

          本文标题:300. Longest Increasing Subseque

          本文链接:https://www.haomeiwen.com/subject/lhssmftx.html