Maximum Number of Eaten Apples

There is a special kind of apple tree that grows apples every day for n days. On the i^th day, the tree grows apples[i] apples that will rot after days[i] days; that is, on day i + days[i] the apples will be rotten and cannot be eaten. On some days, the apple tree does not grow any apples, denoted by apples[i] == 0 and days[i] == 0.

You decided to eat at most one apple a day. Note that you can keep eating after the first n days.

Given two integer arrays days and apples of length n, return the maximum number of apples you can eat.

Example 1
Inputapples = [1,2,3,5,2], days = [3,2,1,4,2]
Output7
By choosing apples before they rot, you can eat 1 apple from day 1, 2 apples from day 2, and 4 apples from day 4, for a total of 7.
Example 2
Inputapples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
Output5
You can eat the 3 apples grown on the first day, do nothing on the fourth and fifth days, then eat the 2 apples grown on the sixth day.

Constraints

  • n == apples.length == days.length
  • 1 <= n <= 2 * 10^4
  • 0 <= apples[i], days[i] <= 2 * 10^4
  • days[i] = 0 if and only if apples[i] = 0.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate