leetcode: rise up sequence

Subject description:

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 increased [2,3,7,101], its length is 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 (n log n) do?

 

Analysis of ideas:

An idea: prompted subject, dynamic programming, can be used complexity of O (N ^ 2) solution of this problem. Dp using a direct array, each memory element with a current maximum sequence rising from the back way, the state transition equation is updated dp [i] = max (dp [i], dp [j] +1), where j is the value of i + 1 to dp.size ().

Thinking two: Due to the advanced requirements are subject needs to be reduced complexity O (NlogN), logn justifies think of using a binary search to reduce this complexity. In fact this idea is to maintain a tail part of the array, iterate nums array, each array to find the tail greater than the current value of the tree, if the replacement, if not then the current value added tail array. The reason for replacement in subsequent lookup, we can find a longer sequence, due to the current tail of elements even smaller. In fact, only when adding new elements will change the size of the longest sequence, so the length of the tail length of the array is maintained at the current longest sequence. Find here the number of uses binary search code directly call the lower_bound () function. Description of this function is as follows:

The first first parameter is a contiguous space of the first address, last address is the end of contiguous space, val is the value to be found. Premise call lower_bound () is this continuous space, the elements are ordered (increment) of.
Then lower_bound () return value is a first value val is greater than or equal address, this address by subtracting the first, is to obtain a first value val is greater than or equal subscript
Also note that another distinction upper_bound function, the return value is greater than a first value val address.
 

Code:

A thought:

 1 class Solution {
 2 public:
 3     int lengthOfLIS(vector<int>& nums) {
 4         if(nums.size()==0)
 5             return 0;
 6         vector<int>dp(nums.size(), 1);
 7         for(int i=nums.size()-1; i>=0; i--)
 8         {
 9             for(int j=i+1; j<nums.size(); j++)
10             {
11                 if(nums[j] > nums[i])
12                 {
13                     dp[i] = max(dp[i], dp[j]+1);
14                 }
15             }
16         }
17         int max = 0;
18         for(int i=0; i<dp.size(); i++)
19         {
20             if(dp[i]>max)
21                 max = dp[i];
22         }
23         return max;
24     }
25 };

Thinking two:

 1 class Solution {
 2 public:
 3     int lengthOfLIS(vector<int>& nums) {
 4         if(nums.size()==0)
 5             return 0;
 6         vector<int> res;
 7         for(int i=0; i<nums.size(); i++)
 8         {
 9             auto iter = lower_bound(res.begin(), res.end(), nums[i]);
10             if(iter == res.end())
11                 res.push_back(nums[i]);
12             else
13                 *iter = nums[i];
14         }
15         return res.size();
16     }
17 };

 

Guess you like

Origin www.cnblogs.com/LJ-LJ/p/11128590.html