Rise longest sequence (LeetCode 300)

The meaning of problems

In the disordered array, increase the longest sequence found

Thinking

This is a dynamic programming problem. And a range which is relevant, this section is called dynamic programming dynamic programming. This state is defined as of a certain range of properties.
and so,

States defined:
DP [i]: to this position i (comprising i), increase the longest sequence

State transition equation:
DP [I] = max (DP [J], J> 0 and J = <I and the nums [I]> the nums [J])

Initial state:
DP [0] =. 1

Code

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        if(nums.empty())
            return 0;
        vector<int> dp(nums.size()+1, 0);
        dp[0] = 1;
        int res = 1;
        for(int i = 1; i < nums.size(); i++)
        {
            int tmp = 0;
            for(int j = 0; j < i; j++)
            {
                if(nums[j] < nums[i])
                {
                    tmp = max(tmp, dp[j]);
                }
            }
            dp[i] = tmp + 1;
            res = max(res, dp[i]);
        }
        return res;
    }
};

Guess you like

Origin www.cnblogs.com/arcpii/p/12533271.html