Drill

ProblemsPython › production

How long to clear the order book

mediumproductionPython

Each order has to be made in one piece on a single day, and the plant has a fixed capacity per day. Orders are taken in the order they were received.

days_to_clear(orders: list<int>, daily_capacity: int) → int

Solve it in the editor →

Where you start

def days_to_clear(orders: list[int], daily_capacity: int) -> int:
    

Worked examples

CallResult
days_to_clear([3, 4, 5], 7)2
days_to_clear([7, 7], 7)2
days_to_clear([8], 7)-1
days_to_clear([1, 1, 1], 10)1

Hint

Track how much of today is left. When the next order does not fit, start a new day rather than splitting it.

Reference solution in Python
def days_to_clear(orders: list[int], daily_capacity: int) -> int:
    if daily_capacity <= 0:
        return -1
    days = 0
    left = 0
    for o in orders:
        if o > daily_capacity:
            return -1
        if o > left:
            days += 1
            left = daily_capacity
        left -= o
    return days

The same problem in another language

More production problems in Python