#1705
Maximum Number of Eaten Apples
specialist · 990 · lc medium +32 · verified · 42.9% accepted · 883 likes · top 24%
Description
An apple tree produces apples for n days. On day i, it grows apples[i] apples that rot on day i + days[i] and cannot be eaten on or after that day. Days with no apples have apples[i] == 0 and days[i] == 0. You eat at most one apple per day and may continue after day n - 1.
Given arrays days and apples of length n, return the maximum number of apples you can eat.
Example 1:
Input: apples = [1,2,3,5,2], days = [3,2,1,4,2]
Output: 7
Explanation: You can eat 7 apples:
- On the first day, you eat an apple that grew on the first day.
- On the second day, you eat an apple that grew on the second day.
- On the third day, you eat an apple that grew on the second day. After this day, the apples that grew on the third day rot.
- On the fourth to the seventh days, you eat apples that grew on the fourth day.
Example 2:
Input: apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
Output: 5
Explanation: You can eat 5 apples:
- On the first to the third day you eat apples that grew on the first day.
- Do nothing on the fouth and fifth days.
- On the sixth and seventh days you eat apples that grew on the sixth day.
Code
1
2
3