Skip to the content

Rum for the voyage

Problem

"One day without rum and this crew mutinies," the cook of the Albatross warns you. You already know how much rum comes aboard or gets drunk on each day of the voyage, and you want to load just enough before sailing. Write a function that takes days, a list of integers (positive when barrels come aboard that day, negative when they go; it may be empty), and returns an integer: the fewest barrels you must sail with so that the count at the end of every day never drops below 0. Reaching 0 is fine. With [3, -5, 2, -4, 6], sailing with 0 the count goes 3, -2, 0, -4 and 2. The lowest point is -4, so you must sail with 4. Note it is not the final total (2) nor the worst day (-5). If the count never drops below 0, or the list is empty, the answer is 0: it is never negative.

Examples

  • The example

    [3, -5, 2, -4, 6] → 4

  • Rum goes out every day

    [-2, -3, -1] → 6

  • Never drops below 0

    [5, -3, 4] → 0

  • Touches 0 and stops there

    [2, -2, 5] → 0

  • The worst day is not the lowest point

    [4, -6, 10, -9, 1] → 2

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def rum_needed(days):
    pass

JavaScript

function rumNeeded(days) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n) challenges

See all challenges →