-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathsol.cpp
58 lines (42 loc) · 1.13 KB
/
sol.cpp
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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
long long maxRunTime(int n, vector<int>& batteries) {
long long total = 0;
for(int battery : batteries) {
total += battery;
}
long long start = 0, end = total;
long long maxTime = 0;
while(start <= end) {
long long mid = start + (end - start) / 2;
if(isFeasible(n, batteries, mid)) {
maxTime = mid;
start = mid + 1;
}
else {
end = mid - 1;
}
}
return maxTime;
}
private:
bool isFeasible(int n, vector<int>& batteries, long long time) {
long long totalRunTime = 0;
for(int battery : batteries) {
if(battery <= time) {
totalRunTime += battery;
}
else {
totalRunTime += time;
}
}
return totalRunTime >= time * n;
}
};
int main() {
// call the fn here
return 0;
}