-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxTime.cpp
More file actions
39 lines (39 loc) · 971 Bytes
/
maxTime.cpp
File metadata and controls
39 lines (39 loc) · 971 Bytes
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
class Solution {
public:
bool isPossible(int n,vector<int>&batteries,long long mid)
{
//here average is taken such that the battery time greater than time is added as equal to time. So that it does not contribute to average.
long long time=0;
for(int i=0;i<batteries.size();++i)
{
if(batteries[i]<mid)
{
time+=batteries[i];
}
else
{
time+=mid;
}
}
return ((time/n)>=mid);
}
long long maxRunTime(int n, vector<int>& batteries) {
long long l=1;
long long h=1e14;
long long ans=0;
while(l<=h)
{
long long mid=l+((h-l)>>1);
if(isPossible(n,batteries,mid))
{
ans=mid;
l=mid+1;
}
else
{
h=mid-1;
}
}
return ans;
}
};