-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum Number of Increments on Subarrays to Form a Target Array.cpp
More file actions
57 lines (56 loc) · 1.38 KB
/
Minimum Number of Increments on Subarrays to Form a Target Array.cpp
File metadata and controls
57 lines (56 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
public:
vector<int> tree;
int build(vector<int>& arr,int start,int end,int i)
{
if(start==end)
return tree[i]=start;
else
{
int q=start+(end-start)/2;
int left=build(arr,start,q,2*i+1);
int right=build(arr,q+1,end,2*i+2);
return tree[i]=arr[left]<arr[right]?left:right;
}
}
int query(vector<int>& arr,int start,int end,int low,int high,int i)
{
if(start<=low&&end>=high)
return tree[i];
else if(high<start||end<low)
return -1;
else
{
int q=(high+low)/2;
int left=query(arr,start,end,low,q,2*i+1);
int right=query(arr,start,end,q+1,high,2*i+2);
if(left==-1)
return right;
if(right==-1)
return left;
return arr[left]<arr[right]?left:right;
}
}
int fun(int subArrVal,int start,int end,vector<int>& target)
{
if(start<=end)
{
int q=query(target,start,end,0,target.size()-1,0);
int val=target[q]-subArrVal;
return val+fun(target[q],start,q-1,target)+fun(target[q],q+1,end,target);
}
else return 0;
}
int minNumberOperations(vector<int>& target)
{
int n=target.size();
int x = (int)(ceil(log2(n)));
int max_size = 2*(int)pow(2, x) - 1;
tree.resize(max_size,-1);
build(target,0,n-1,0);
return fun(0,0,n-1,target);
for(int x:tree)
cout<<x<<endl;
return 0;
}
};