1490. Leaky Bucket Levels
A row of water tanks holds buckets[i] litres in tank i. Water can be moved freely between tanks, but the hose leaks: when w litres are poured out of one tank, only w reduced by lossPercent percent arrives in the other. Tanks may be emptied completely, and the leaked water is gone for good.
The manager wants every tank to end with exactly the same level. Return the largest level L (in litres, not necessarily whole) that can be reached this way. Tanks above the level are the donors and tanks below it receive, and the water received in total must not exceed what survives the leak. With a single tank the answer is its own level. The answer is accepted with absolute or relative error 1e-5. Binary search on L runs in O(n log(1/eps)) time and O(1) space.
Example 1
- Input:
- buckets = [2,6], lossPercent = 0
- Output:
- 4
- Explanation:
With no leak the water just evens out to the average, 4 litres.
Example 2
- Input:
- buckets = [0,10], lossPercent = 50
- Output:
- 3.333333333333333
- Explanation:
Pouring x litres out of the full tank delivers x/2, so 10 - x = x/2 gives a level of 10/3.
Example 3
- Input:
- buckets = [7], lossPercent = 30
- Output:
- 7
- Explanation:
A single tank keeps its level, 7.
Constraints
1 ≤ n ≤ 1000
0 ≤ buckets[i] ≤ 105
0 ≤ lossPercent ≤ 99
Answers within an absolute or relative error of 1e-5 are accepted.
How this problem is judged
- Answers
- Numbers are accepted within a tolerance of 1.0E-5: |answer - expected| <= 1.0E-5 x max(1, |expected|).
- Tolerance
0.00001- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(n log(1/eps))
- Space
- O(1)