-
Notifications
You must be signed in to change notification settings - Fork 64
/
sol.java
42 lines (37 loc) · 1.03 KB
/
sol.java
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
public class sol {
public static void main(String[] args) {
// call the fn here
}
class Solution {
public long maxRunTime(int n, int[] batteries) {
long sum = 0;
for (int x : batteries) {
sum += x;
}
long stTime = 0;
long endTime = sum;
long ans = 0;
while (stTime <= endTime) {
long avg = (stTime + endTime) / 2;
if (runFeasible(n, batteries, avg)) {
ans = avg;
stTime = avg + 1;
} else {
endTime = avg - 1;
}
}
return ans;
}
public boolean runFeasible(int n, int[] batteries, long avg) {
long time = 0;
for (int x : batteries) {
if (x <= avg) {
time += x;
} else {
time += avg;
}
}
return time >= avg * n;
}
}
}