When going through the discuss section, I found several different solutions for this problem, one using the standard DP (O(N^2))
, one using binary search (O(NlogN))
, The last one is tricky using lower_bound (O(NlogN))
. And my own solution is pretty easy to come up with, however, the worst time complexity is still O(N^2)
.
Let’s have a look at my own solution first.
int lengthOfLIS(vector<int>& nums) {
int res = 0;
if(nums.empty()) return 0;
for(int i = 1; i<nums.size()-res+1; i++){
res = max(helper(nums),res);
nums.erase(nums.begin(),nums.begin()+i);
}
return res;
}
int helper(vector<int>& nums) {
vector<int> dp(nums.size(),1);
int pre = INT_MIN,last = nums[0];
for(int i = 1; i<nums.size(); i++){
if(nums[i] > pre && nums[i]<last){
last = nums[i];
dp[i] = dp[i-1];
} else if(nums[i] > last){
pre = last;
last = nums[i];
dp[i] = dp[i-1]+1;
} else dp[i] = dp[i-1];
}
return dp[nums.size()-1];
}
The basic idea for this solution is DP
, we keep tracking the last two elements in current result array. For example, let’s say the current result array is [2,3,10]
, last
refers to the last element 10
, pre
refers to the second last element 3
. when considering the next element nums[i]
, there’re three possibilities:
(1) nums[i] > last
, we simply add nums[i]
into the result array, dp[i] = dp[i-1]+1
, then we update the last
and pre
.
(2) nums[i] > pre && nums[i]<last
, in this case, we know that nums[i]
should replace the last element.
(3) otherwise dp[i] = dp[i-1]
The most important step which can be missed easily is that we need to iterate the whole nums array to collect all possible LIS. Right after each iteration, we erase the first element in the nums array to generate a possible new start point for the result array. And we only need to consider the array with start position lower than nums.size()-res
, since for array with length smaller than res
, it can’t contain a longer LIS than res
.
Okay that’s pretty much for my own stupid solution.
Let’s check the standard DP
solution.
int lengthOfLIS(vector<int>& nums) {
// O(N^2)
if(nums.empty()) return 0;
int res = 1;
vector<int> dp(nums.size(),1);
for(int i = 1; i<nums.size(); i++){
for(int j = 0; j<i; j++){
if(nums[j] < nums[i])
dp[i] = max(dp[i],dp[j]+1);
}
res = max(res,dp[i]);
}
return res;
}
The basic idea for this solution is to update the dp[i]
when iterating the subarray with length i
.
The tricky one – lower_bound
int lengthOfLIS(vector<int>& nums) {
/* O(NlogN) */
vector<int> sub;
for (auto a : nums) {
/* find the first elmt not less than a*/
auto i = lower_bound(sub.begin(), sub.end(), a);
// /if all elmt less than a, add a into the result array
if (i == sub.end()) sub.push_back(a);
// otherwise, replace the first not-less-than a elmt with a
else *i = a;
}
return sub.size();
}
Binary Search
!
int lengthOfLIS(vector<int>& nums) {
vector<int> LIS;
for (int i = 0; i < nums.size(); i++) {
if (LIS.size() == 0 || LIS[LIS.size() - 1] < nums[i])
LIS.push_back(nums[i]);
else {
int l = 0, r = LIS.size() - 1;
/* find the first elmt not less than nums[i] using BS
then replace the it with nums[i]*/
while (l < r) {
int m = (l + r) / 2;
if (LIS[m] >= nums[i])
r = m;
else l = m + 1;
}
LIS[l] = nums[i];
}
}
return LIS.size();
}
The idea is same with the lower_bound one.