You have n buckets each containing some gallons of water in it, represented by a 0-indexed integer array buckets, where the ith bucket contains
buckets[i] gallons of water. You are also given an integer loss.
You want to make the amount of water in each bucket equal. You can pour any amount of water from one bucket to another bucket (not necessarily an integer). However, every time you pour k gallons of water, you spill losspercent of k.
Return _themaximum amount of water in each bucket after making the amount of water equal. _Answers within 10-5 of the actual answer will be accepted.
Input: buckets =[1,2,7], loss =80Output: 2.00000Explanation: Pour 5 gallons of water from buckets[2] to buckets[0].5*80%=4 gallons are spilled and buckets[0] only receives 5-4=1 gallon of water.All buckets have 2 gallons of water in them so return2.
Example 2:
1
2
3
4
5
6
7
8
Input: buckets =[2,4,6], loss =50Output: 3.50000Explanation: Pour 0.5 gallons of water from buckets[1] to buckets[0].0.5*50%=0.25 gallons are spilled and buckets[0] only receives 0.5-0.25=0.25 gallons of water.Now, buckets =[2.25,3.5,6].Pour 2.5 gallons of water from buckets[2] to buckets[0].2.5*50%=1.25 gallons are spilled and buckets[0] only receives 2.5-1.25=1.25 gallons of water.All buckets have 3.5 gallons of water in them so return3.5.
Example 3:
1
2
3
Input: buckets =[3,3,3,3], loss =40Output: 3.00000Explanation: All buckets already have the same amount of water in them.
We want to maximize the equal water level after pouring, accounting for loss. If loss = 0, the answer is the average. If loss > 0, pouring from a higher bucket to a lower one loses water, so the answer is less than the average. We can use binary search on the target level. For a candidate mid level, compute the total need (amount required to bring buckets below mid up) and the total give (amount we can pour from buckets above mid after keeping keep = 1 - loss/100 fraction). If give >= need, the level is feasible.
Use binary search between l = 0 and r = max(buckets) to find the maximum possible level.
For each candidate mid, compute need = sum(max(0, mid - x) for x in buckets) and give = sum(max(0, x - mid) * keep for x in buckets) where keep = 1 - loss / 100.0.
If give >= need, the level mid is feasible; move l to mid and record ans = mid.
Otherwise move r to mid.
Repeat for sufficient iterations (e.g., 100) and return ans with the required precision.
#include<vector>#include<algorithm>usingnamespace std;
doubleequalizeWater(vector<int>& buckets, int loss) {
double l =0, r =*max_element(buckets.begin(), buckets.end()), ans =0;
double eps =1e-6;
double keep =1- loss /100.0;
for (int iter =0; iter <100; ++iter) {
double mid = (l + r) /2;
double need =0, give =0;
for (int x : buckets) {
if (x < mid) need += mid - x;
else give += (x - mid) * keep;
}
if (give >= need) {
ans = mid; l = mid;
} else {
r = mid;
}
}
return ans;
}
classSolution {
publicdoubleequalizeWater(int[] buckets, int loss) {
double l = 0, r = 0, ans = 0;
for (int x : buckets) r = Math.max(r, x);
double keep = 1 - loss / 100.0;
for (int iter = 0; iter < 100; ++iter) {
double mid = (l + r) / 2;
double need = 0, give = 0;
for (int x : buckets) {
if (x < mid) need += mid - x;
else give += (x - mid) * keep;
}
if (give >= need) {
ans = mid; l = mid;
} else {
r = mid;
}
}
return ans;
}
}
funequalizeWater(buckets: IntArray, loss: Int): Double {
var l = 0.0var r = buckets.maxOrNull()!!.toDouble()
var ans = 0.0val keep = 1 - loss / 100.0 repeat(100) {
val mid = (l + r) / 2var need = 0.0var give = 0.0for (x in buckets) {
if (x < mid) need += mid - x
else give += (x - mid) * keep
}
if (give >= need) {
ans = mid; l = mid
} else {
r = mid
}
}
return ans
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
defequalizeWater(buckets: list[int], loss: int) -> float:
l, r =0.0, max(buckets)
ans =0.0 keep =1- loss /100.0for _ in range(100):
mid = (l + r) /2 need = sum(max(0, mid - x) for x in buckets)
give = sum(max(0, x - mid) * keep for x in buckets)
if give >= need:
ans = mid
l = mid
else:
r = mid
return ans
fnequalize_water(buckets: Vec<i32>, loss: i32) -> f64 {
letmut l =0.0;
letmut r =*buckets.iter().max().unwrap() asf64;
letmut ans =0.0;
let keep =1.0- (loss asf64) /100.0;
for _ in0..100 {
let mid = (l + r) /2.0;
letmut need =0.0;
letmut give =0.0;
for&x in&buckets {
if (x asf64) < mid {
need += mid - (x asf64);
} else {
give += ((x asf64) - mid) * keep;
}
}
if give >= need {
ans = mid;
l = mid;
} else {
r = mid;
}
}
ans
}