Ctrl K

Can the Shelves Be Emptied

Medium
A warehouse has a row of shelves, and shelf i holds stock[i] items. Trips windows arrive one after another, where windows[j] = [l, r]: the trip passes shelves l through r inclusive and may take one item from any of them, whether that is one shelf, several, all of them or none. Return True if all shelves can be left empty after every trip, and False otherwise.
Example 1
stock=[1, 2, 0, 1], windows=[[0, 1], [1, 3]] → True

The first trip takes one item from shelves 0 and 1, the second one from shelves 1 and 3, skipping the empty shelf 2. Every shelf ends up empty.

Example 2
stock=[2, 1], windows=[[0, 1]] → False

Shelf 0 holds two items, but there is only one trip, so just one of them can be taken.

Constraints
  • 1 ≤ len(stock) ≤ 10⁵
  • 0 ≤ stock[i] ≤ 10⁵
  • 1 ≤ len(windows) ≤ 10⁵
  • windows[j] = [l, r], 0 ≤ l ≤ r < len(stock)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Stretch to the Goal MediumProduct of All the Others MediumStudios for Recording Sessions MediumLongest Run with a Given Sum MediumBatch of Segment Adjustments Medium
loading Python…
Input
stock=[1, 2, 0, 1], windows=[[0, 1], [1, 3]]
Expected
True

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