Skip to content

Practice Maximum Number of Eaten Apples

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1705
LeetCode Problem

Maximum Number of Eaten Apples

Problem Statement

There is a special kind of apple tree that grows apples every day for n days. On the ith 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, which are denoted by apples[i] == 0 and days[i] == 0. You decided to eat at most one apple a day (to keep the doctors away). 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.

Source: Maximum Number of Eaten Apples on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
apples = [1,2,3,5,2], days = [3,2,1,4,2]
Output
7

# Example 2

Input
apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
Output
5

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.

Related LeetCode Problems

Practice more problems similar to Maximum Number of Eaten Apples.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.