-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
48 lines (40 loc) · 1.26 KB
/
Copy pathsolution.java
File metadata and controls
48 lines (40 loc) · 1.26 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
class Solution {
private boolean canMake(int[] arr, int n, int k, int w, long target) {
long[] diff = new long[n + 1];
long currAdd = 0;
long operations = 0;
for (int i = 0; i < n; i++) {
currAdd += diff[i];
long currentHeight = arr[i] + currAdd;
if (currentHeight < target) {
long need = target - currentHeight;
operations += need;
if (operations > k)
return false;
currAdd += need;
if (i + w < n)
diff[i + w] -= need;
}
}
return true;
}
public int maxMinHeight(int[] arr, int k, int w) {
int n = arr.length;
int minVal = Integer.MAX_VALUE;
for (int val : arr)
minVal = Math.min(minVal, val);
long low = minVal;
long high = minVal + k;
long ans = minVal;
while (low <= high) {
long mid = (low + high) / 2;
if (canMake(arr, n, k, w, mid)) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return (int) ans;
}
}