-
Notifications
You must be signed in to change notification settings - Fork 0
/
8th March 2023
101 lines (51 loc) · 1.63 KB
/
8th March 2023
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
class Solution
{
public:
bool isCheck(vector<int> &arr, int days, int cs, long long int h){
int n=arr.size();
vector<long long> waterSupply(n,0);
if(arr[0]<h){
waterSupply[0]=h-arr[0];
days-=(h-arr[0]);
}
if(days<0){
return false;
}
for(int i=1;i<n;i++){
waterSupply[i]=waterSupply[i-1];
int actualHeight=arr[i];
if(i>=cs){
actualHeight+=(waterSupply[i]-waterSupply[i-cs]);
}
else{
actualHeight+=waterSupply[i];
}
if(actualHeight<h){
waterSupply[i]+=(h-actualHeight);
days-=(h-actualHeight);
}
if(days<0){
return false;
}
}
return true;
}
long long int maximizeMinHeight(int n,int k,int w,vector <int> &a)
{
// code here
long long int ans=-1;
long long int mini=*min_element(a.begin(),a.end());
long long int maxi=INT_MAX/2;
while(mini<=maxi){
int guessHeight=(maxi+mini)/2;
if(isCheck(a,k,w,guessHeight)==true){
ans=guessHeight;
mini=guessHeight+1;
}
else{
maxi=guessHeight-1;
}
}
return ans;
}
};