Ctrl K

Eat Before They Spoil

Medium
A berry bush bears fruit for n days in a row. On day i (0-based) you pick harvest[i] berries from it, and they stay fresh for shelf_life[i] days: on day i + shelf_life[i] they have already gone bad. On days with no berries, harvest[i] = 0 and shelf_life[i] = 0. You eat at most one berry a day, and you may keep eating after the bush stops bearing fruit. Return the largest number of berries you can eat.
Example 1
harvest=[2, 3], shelf_life=[5, 1] → 3

Berries from the first picking are good on days 0–4, from the second only on day 1. On day 0 eat one from the first picking, on day 1 one from the second (its other two go bad), on day 2 the last one from the first. That is 3.

Example 2
harvest=[1, 0, 0, 2], shelf_life=[1, 0, 0, 4] → 3

The single berry of day 0 is eaten right away. Days 1 and 2 have nothing to eat. The two berries of day 3 stay good until day 6 and are eaten on days 3 and 4.

Example 3
harvest=[4, 1, 1], shelf_life=[2, 4, 1] → 4
Constraints
  • 1 ≤ n ≤ 2 · 10⁴
  • len(harvest) = len(shelf_life) = n
  • 0 ≤ harvest[i], shelf_life[i] ≤ 2 · 10⁴
  • shelf_life[i] = 0 if and only if harvest[i] = 0
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStudios for Recording Sessions MediumJobs With a Cooldown MediumSorted Into Runs of Three MediumSpread Out the Labels MediumSplicing Ropes Medium
loading Python…
Input
harvest=[2, 3], shelf_life=[5, 1]
Expected
3

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