Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/
- 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
- Solution (INCASE QUESTION GOES PREMIUM) - https://drive.google.com/drive/folders/1GRA02LA4qBt8A1N48UWqjhR-KtS1Txr0?usp=sharing
- You are given an integer array target. You have an integer array initial of the same size as target with all elements initially zeros.
- In one operation you can choose any subarray from initial and increment each value by one.
- Return the minimum number of operations to form a target array from initial.
- The test cases are generated so that the answer fits in a 32-bit integer
- Example 1:
- Input: target = [1,2,3,2,1]
- Output: 3
- Explanation: We need at least 3 operations to form the target array from the initial array.
- [0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
- [1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
- [1,2,2,2,1] increment 1 at index 2.
- [1,2,3,2,1] target array is formed.
- Example 2:
- Input: target = [3,1,1,2]
- Output: 4
- Explanation: [0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2]
- Example 3:
- Input: target = [3,1,5,4,2]
- Output: 7
- 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].
- Constraints:
- 1 <= target.length <= 10^5
- 1 <= target[i] <= 10^5
- ------------------------------------------------------------------------------------------------------------------------------------
- CODE 1:
- int minNumberOperations(vector<int>& a) {
- int n=a.size();
- vector<int> diff=a;
- for(int i=1;i<n;i++){
- diff[i]=a[i]-a[i-1];
- }
- for(auto e: diff){
- cout<<e<<" ";
- }
- cout<<endl;
- int cnt=0;
- for(auto ele : diff){
- if(ele>0){
- cnt+=ele;
- }
- }
- return cnt;
- } // O(N) SOLUTION O(1) SPACE
- ------------------------------------------------------------------------------------------------------------------------------------
- CODE 2:
- int minNumberOperations(vector<int>& target) {
- stack<int> mono_incr;
- int ans=0;
- for(int i=0;i<target.size();i++){
- if(!mono_incr.empty() && mono_incr.top()>=target[i]){
- ans+=mono_incr.top()-target[i];
- mono_incr.pop();
- }
- mono_incr.push(target[i]);
- }
- ans+=mono_incr.top();
- return ans;
- } // O(N) monotonic stack
- ------------------------------------------------------------------------------------------------------------------------------------
- CODE 3:
- vector<int> tree;
- void build(vector<int> &arr, int start, int end, int parent){
- if(start==end){
- tree[parent]=start;
- return;
- }
- int q=(end+start)/2;
- build(arr,start,q,2*parent+1);
- build(arr,q+1,end,2*parent+2);
- tree[parent]=arr[tree[2*parent+1]]<arr[tree[2*parent+2]] ? tree[2*parent+1] : tree[2*parent+2];
- }
- int query(vector<int> &arr, int start, int end, int low, int high, int parent){
- if(start<=low && end>=high){
- return tree[parent];
- }
- if(high<start || end<low){
- return INT_MAX;
- }
- int mid=(high+low)/2;
- int left=query(arr,start,end,low,mid,2*parent+1);
- int right=query(arr,start,end,mid+1,high,2*parent+2);
- if(left==INT_MAX || right==INT_MAX){
- return right==INT_MAX ? left : right;
- }
- return arr[left]<arr[right] ? left : right;
- }
- int helper(int latestUpdated, int start, int end, vector<int> &target){
- if(start>end){ // it is not a valid subarray
- return 0;
- }
- int index=query(target,start,end,0,target.size()-1,0);
- int ops=target[index]-latestUpdated;
- return ops+helper(target[index],start,index-1,target)+helper(target[index],index+1,end,target);
- // placing target[index] in latest Updated cuz remember, we filled the whole array with target[index] element
- }
- int minNumberOperations(vector<int> &target) {
- // we are filling an imaginary array of zeros to target. Filling this [0,0,0...] to target
- int n=target.size();
- tree.resize(4*n,-1);
- build(target,0,n-1,0);
- return helper(0,0,n-1,target);
- }
Advertisement
Add Comment
Please, Sign In to add comment