-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 139.java
More file actions
55 lines (41 loc) · 1.36 KB
/
Day 139.java
File metadata and controls
55 lines (41 loc) · 1.36 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
49
50
51
52
53
54
55
class Solution {
public int maxMinHeight(int[] arr, int k, int w) {
int n = arr.length;
long low = Integer.MAX_VALUE;
long high = (long)1e14;
for (int height : arr) {
low = Math.min(low, height);
}
long ans = low;
while (low <= high) {
long mid = low + (high - low) / 2;
if (canAchieve(arr, k, w, mid)) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return (int) ans;
}
private boolean canAchieve(int[] arr, int k, int w, long target) {
int n = arr.length;
long[] diff = new long[n + 1];
long waterUsed = 0;
long currAdd = 0;
for (int i = 0; i < n; i++) {
currAdd += diff[i];
long currentHeight = arr[i] + currAdd;
if (currentHeight < target) {
long need = target - currentHeight;
waterUsed += need;
if (waterUsed > k) return false;
currAdd += need;
if (i + w < n) {
diff[i + w] -= need;
}
}
}
return true;
}
}