Ctrl K

Emptying Filling Tanks

Hard
There are n tanks: tank i holds levels[i] litres right now, and rates[i] litres flow into it every second. Each second goes like this: first water flows into all tanks at once, then you may pick one tank and empty it completely. Return the fewest seconds after which the total amount of water in all tanks can be at most limit, or -1 if that can never happen. If the total is already at most limit, the answer is 0.
Example 1
levels=[4, 1, 2], rates=[1, 3, 0], limit=5 → 2

Second 1: levels [5, 4, 2], empty tank 1 to get [5, 0, 2], total 7. Second 2: [6, 3, 2], empty tank 0 to get [0, 3, 2], total 5. In one second the best possible total is 6.

Example 2
levels=[2, 2], rates=[5, 5], limit=3 → -1

After every second the tank not emptied in that second holds at least 5, so the total never gets down to 3.

Example 3
levels=[3, 4], rates=[1, 1], limit=10 → 0

The total of 7 is already within 10, so no time is needed.

Constraints
  • 1 ≤ n = len(levels) = len(rates) ≤ 1000
  • 1 ≤ levels[i] ≤ 1000
  • 0 ≤ rates[i] ≤ 1000
  • 0 ≤ limit ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBoxes Inside Boxes HardStrict Climb From Spare Parts HardBest-Paying Set of Gigs HardLongest Downhill Route HardBiggest Number Divisible by Three Hard
loading Python…
Input
levels=[4, 1, 2], rates=[1, 3, 0], limit=5
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 114 tests on the server, hidden ones included.