Ctrl K

Exact Drops to Zero

Medium
You get an array of non-negative integers levels and a list of steps drops, where drops[j] = [l, r, val]. In a step you may pick any set of cells with indices from l to r inclusive (possibly none) and lower each picked cell by exactly val. Going below zero is not allowed: every cell has to land exactly on zero. Steps are applied strictly in order. Find the smallest k ≥ 0 such that after the first k steps the array can be made all zeros. If that is impossible even with every step, return -1.
Example 1
levels=[3, 2], drops=[[0, 1, 2], [0, 0, 1], [1, 1, 5], [0, 1, 1]] → 2

The first step takes exactly 2 from both cells, giving [1, 0]. The second takes 1 from cell 0, and the array is all zeros, so the answer is 2.

Example 2
levels=[4], drops=[[0, 0, 3], [0, 0, 3]] → -1

Steps of 3 can only add up to 0, 3 or 6, never exactly 4.

Example 3
levels=[0, 0, 0], drops=[[0, 2, 1]] → 0

The array is already all zeros, so the answer is 0.

Constraints
  • 1 ≤ len(levels) ≤ 10
  • 0 ≤ levels[i] ≤ 1000
  • 1 ≤ len(drops) ≤ 1000
  • drops[j] = [l, r, val], 0 ≤ l ≤ r < len(levels)
  • 1 ≤ val ≤ 10
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route Medium
loading Python…
Input
levels=[3, 2], drops=[[0, 1, 2], [0, 0, 1], [1, 1, 5], [0, 1, 1]]
Expected
2

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