Leetcode-] [Dynamic Programming Longest Increasing Subsequence

A disorder of a given integer array, the length of the longest found rising sequence.

Example:

Input:  [10,9,2,5,3,7,101,18]
Output: 4 
 explained: the longest sequence is increase  [2,3,7,101],its length 4.

Description:

  • Various combinations may be increased up sequence, you only need to output a corresponding length.
  • The time complexity of your algorithm should be O ( n2 ).

Advanced:  You can reduce the time complexity of the algorithm to O ( the n-  log  the n- ) do?


 

Solution a: Complexity o (n2)

class Solution {
     public int lengthOfLIS(int[] nums) {
        int[] dp = new int[nums.length];
        for(int i=0;i<dp.length;i++) {
            dp[i] =1;
        }
        int res = 0;

        for (int i = 0; i < nums.length; i++) {
            for(int j=0;j<i;j++) {
                if(nums[i]>nums[j]) {
                    dp[i] = Math.max(dp[i], dp[j]+1);
                }
            }

            res = Math.max(res, dp[i]);
        }

        return res;
    }
}

 

Published 229 original articles · won praise 245 · views 310 000 +

Guess you like

Origin blog.csdn.net/kangbin825/article/details/105324583
Recommended