300. Longest Increasing Subsequence

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();
    }
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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值