Drill

ProblemsC# › production

How long to clear the order book

mediumproductionArraysGreedySimulationC#

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.

DaysToClear(orders: list<int>, dailyCapacity: int) → int

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int DaysToClear(List<int> orders, int dailyCapacity) {
    
}

Worked examples

CallResult
DaysToClear(new List<int> { 3, 4, 5 }, 7)2
DaysToClear(new List<int> { 7, 7 }, 7)2
DaysToClear(new List<int> { 8 }, 7)-1
DaysToClear(new List<int> { 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 C#
public int DaysToClear(List<int> orders, int dailyCapacity) {
    if (dailyCapacity <= 0) return -1;
    int days = 0, left = 0;
    foreach (var o in orders) {
        if (o > dailyCapacity) return -1;
        if (o > left) { days++; left = dailyCapacity; }
        left -= o;
    }
    return days;
}

The same problem in another language

More production problems in C#