RainX_69

2407. Longest Increasing Subsequence II

Jan 2nd, 2023 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | Source Code | 0 0
  1. 2407. Longest Increasing Subsequence II
  2. https://leetcode.com/problems/longest-increasing-subsequence-ii/
  3.  
  4. You are given an integer array nums and an integer k.
  5.  
  6. Find the longest subsequence of nums that meets the following requirements:
  7. -> The subsequence is strictly increasing and
  8. -> The difference between adjacent elements in the subsequence is at most k.
  9.  
  10. Return the length of the longest subsequence that meets the requirements.
  11.  
  12. A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
  13.  
  14.  
  15.  
  16. Example 1:
  17.  
  18. Input: nums = [4,2,1,4,3,4,5,8,15], k = 3
  19. Output: 5
  20. Explanation:
  21. The longest subsequence that meets the requirements is [1,3,4,5,8].
  22. The subsequence has a length of 5, so we return 5.
  23. Note that the subsequence [1,3,4,5,8,15] does not meet the requirements because 15 - 8 = 7 is larger than 3.
  24.  
  25. Example 2:
  26.  
  27. Input: nums = [7,4,5,1,8,12,4,7], k = 5
  28. Output: 4
  29. Explanation:
  30. The longest subsequence that meets the requirements is [4,5,8,12].
  31. The subsequence has a length of 4, so we return 4.
  32.  
  33. Example 3:
  34.  
  35. Input: nums = [1,5], k = 1
  36. Output: 1
  37. Explanation:
  38. The longest subsequence that meets the requirements is [1].
  39. The subsequence has a length of 1, so we return 1.
  40.  
  41.  
  42. Constraints:
  43.  
  44. 1 <= nums.length <= 10^5
  45. 1 <= nums[i], k <= 10^5
  46.  
  47.  
  48. ---------------------------------------------------------------------------------------------------------------------------------------
  49.  
  50. BRUTE FORCE (TLE)
  51.  
  52. class Solution {
  53. public:
  54.     int lengthOfLIS(vector<int>& nums, int k) {
  55.         int n=nums.size();
  56.         vector<int> dp(n,1);
  57.         for(int end=1;end<n;end++){
  58.             for(int start=0;start<end;start++){
  59.                 if(nums[end]>nums[start] && nums[end]-nums[start]<=k){
  60.                     dp[end]=max(dp[end],dp[start]+1);
  61.                 }
  62.             }
  63.         }
  64.         return *max_element(dp.begin(),dp.end());
  65.     }
  66. };
  67.  
  68.  
  69. --------------------------------------------------------------------------------------------------------------------------------------
  70.  
  71.  
  72. SEGMENT TREE (AC)
  73.  
  74. class Solution {
  75. private:
  76.     const static int MAX=100001;
  77.     int tree[4*MAX];
  78. public:
  79.     void update(int start, int end, int parent, int updateVal, int idx){
  80.         if(start==end){
  81.             tree[parent]=updateVal;
  82.             return;
  83.         }
  84.         int mid=(start+end)/2;
  85.         if(idx<=mid){
  86.             update(start,mid,2*parent+1,updateVal,idx);
  87.         }
  88.         else{
  89.             update(mid+1,end,2*parent+2,updateVal,idx);
  90.         }
  91.         tree[parent]=max(tree[2*parent+1],tree[2*parent+2]);
  92.     }
  93.    
  94.     int maxLen(int start, int end, int l, int r, int parent){
  95.         if(start>r || end<l){
  96.             return 0;
  97.         }
  98.         if(start>=l && end<=r){
  99.             return tree[parent];
  100.         }
  101.         int mid=(start+end)/2;
  102.         int left=maxLen(start,mid,l,r,2*parent+1);
  103.         int right=maxLen(mid+1,end,l,r,2*parent+2);
  104.         return max(left,right);
  105.     }
  106.    
  107.     int lengthOfLIS(vector<int>& nums, int k){
  108.         memset(tree,0,sizeof(tree));
  109.         int res=1;
  110.         for(int i=0;i<nums.size();i++){
  111.             if(nums[i]==1){
  112.                 update(1,MAX,0,1,nums[i]); // because less than 1 not possible
  113.                 continue;
  114.             }
  115.             int best=maxLen(1,MAX,max(nums[i]-k,1),nums[i]-1,0)+1; // we are finding the best in range [max(1,nums[i]-k),nums[i]-1] and adding nums[i] to it
  116.             update(1,MAX,0,best,nums[i]);  // update the best answer you got for nums[i]. Remember each element in this array is acting as an index in a segment tree
  117.             res=max(res,best);
  118.         }
  119.         return res;        
  120.     }
  121. };
Advertisement
Add Comment
Please, Sign In to add comment