RainX_69

Minimum Number of Increments on Subarrays to Form a Target Array | MUST DO | HARD | SEGMENT TREE

Feb 28th, 2023 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | Source Code | 0 0
  1. https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/
  2.  
  3. Solution - https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/discuss/2211252/C%2B%2B-Code-For-Beginners-or-SEGMENT-TREE-SOLUTION-EXPLAINED
  4.  
  5. Solution (INCASE QUESTION GOES PREMIUM) - https://drive.google.com/drive/folders/1GRA02LA4qBt8A1N48UWqjhR-KtS1Txr0?usp=sharing
  6.  
  7. You are given an integer array target. You have an integer array initial of the same size as target with all elements initially zeros.
  8. In one operation you can choose any subarray from initial and increment each value by one.
  9. Return the minimum number of operations to form a target array from initial.
  10.  
  11. The test cases are generated so that the answer fits in a 32-bit integer
  12.  
  13. Example 1:
  14. Input: target = [1,2,3,2,1]
  15. Output: 3
  16. Explanation: We need at least 3 operations to form the target array from the initial array.
  17. [0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
  18. [1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
  19. [1,2,2,2,1] increment 1 at index 2.
  20. [1,2,3,2,1] target array is formed.
  21.  
  22. Example 2:
  23. Input: target = [3,1,1,2]
  24. Output: 4
  25. Explanation: [0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2]
  26.  
  27. Example 3:
  28. Input: target = [3,1,5,4,2]
  29. Output: 7
  30. Explanation: [0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1] -> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2].
  31.  
  32.  
  33. Constraints:
  34.  
  35. 1 <= target.length <= 10^5
  36. 1 <= target[i] <= 10^5
  37. ------------------------------------------------------------------------------------------------------------------------------------
  38.  
  39. CODE 1:
  40.  
  41.     int minNumberOperations(vector<int>& a) {
  42.         int n=a.size();
  43.         vector<int> diff=a;
  44.         for(int i=1;i<n;i++){
  45.             diff[i]=a[i]-a[i-1];
  46.         }
  47.         for(auto e: diff){
  48.             cout<<e<<" ";
  49.         }
  50.         cout<<endl;
  51.         int cnt=0;
  52.         for(auto ele : diff){
  53.             if(ele>0){
  54.                 cnt+=ele;
  55.             }
  56.         }
  57.         return cnt;
  58.     }  // O(N) SOLUTION O(1) SPACE
  59. ------------------------------------------------------------------------------------------------------------------------------------
  60.  
  61. CODE 2:
  62.    
  63.     int minNumberOperations(vector<int>& target) {
  64.         stack<int> mono_incr;
  65.         int ans=0;
  66.         for(int i=0;i<target.size();i++){
  67.             if(!mono_incr.empty() && mono_incr.top()>=target[i]){
  68.                 ans+=mono_incr.top()-target[i];
  69.                 mono_incr.pop();
  70.             }
  71.             mono_incr.push(target[i]);
  72.         }
  73.         ans+=mono_incr.top();
  74.         return ans;
  75.     }  // O(N) monotonic stack
  76.    
  77. ------------------------------------------------------------------------------------------------------------------------------------
  78.  
  79. CODE 3:
  80.  
  81.     vector<int> tree;
  82.    
  83.     void build(vector<int> &arr, int start, int end, int parent){
  84.         if(start==end){
  85.             tree[parent]=start;
  86.             return;
  87.         }
  88.         int q=(end+start)/2;
  89.         build(arr,start,q,2*parent+1);
  90.         build(arr,q+1,end,2*parent+2);
  91.         tree[parent]=arr[tree[2*parent+1]]<arr[tree[2*parent+2]] ? tree[2*parent+1] : tree[2*parent+2];
  92.     }
  93.  
  94.     int query(vector<int> &arr, int start, int end, int low, int high, int parent){
  95.         if(start<=low && end>=high){
  96.             return tree[parent];
  97.         }
  98.         if(high<start || end<low){
  99.             return INT_MAX;
  100.         }
  101.         int mid=(high+low)/2;
  102.         int left=query(arr,start,end,low,mid,2*parent+1);
  103.         int right=query(arr,start,end,mid+1,high,2*parent+2);
  104.         if(left==INT_MAX || right==INT_MAX){
  105.             return right==INT_MAX ? left : right;
  106.         }
  107.         return arr[left]<arr[right] ? left : right;    
  108.     }
  109.    
  110.     int helper(int latestUpdated, int start, int end, vector<int> &target){
  111.         if(start>end){  // it is not a valid subarray
  112.             return 0;
  113.         }
  114.         int index=query(target,start,end,0,target.size()-1,0);
  115.         int ops=target[index]-latestUpdated;
  116.         return ops+helper(target[index],start,index-1,target)+helper(target[index],index+1,end,target);
  117.         // placing target[index] in latest Updated cuz remember, we filled the whole array with target[index] element
  118.     }
  119.    
  120.     int minNumberOperations(vector<int> &target) {
  121.         // we are filling an imaginary array of zeros to target. Filling this [0,0,0...] to target
  122.         int n=target.size();
  123.         tree.resize(4*n,-1);
  124.         build(target,0,n-1,0);
  125.         return helper(0,0,n-1,target);
  126.     }
  127.  
  128.  
  129.  
Advertisement
Add Comment
Please, Sign In to add comment