Drill

ProblemsTypeScript › production

How long to clear the order book

mediumproductionTypeScript

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

Solve it in the editor →

Where you start

function daysToClear(orders: number[], dailyCapacity: number): number {
  
}

Worked examples

CallResult
daysToClear([3,4,5], 7)2
daysToClear([7,7], 7)2
daysToClear([8], 7)-1
daysToClear([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 TypeScript
function daysToClear(orders: number[], dailyCapacity: number): number {
  if (dailyCapacity <= 0) return -1;
  let days = 0;
  let left = 0;
  for (const o of 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 TypeScript