Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 36e774f

Browse files
committed
Add solution #1705
1 parent 29b7fe0 commit 36e774f

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,489 LeetCode solutions in JavaScript
1+
# 1,490 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1314,6 +1314,7 @@
13141314
1702|[Maximum Binary String After Change](./solutions/1702-maximum-binary-string-after-change.js)|Medium|
13151315
1703|[Minimum Adjacent Swaps for K Consecutive Ones](./solutions/1703-minimum-adjacent-swaps-for-k-consecutive-ones.js)|Hard|
13161316
1704|[Determine if String Halves Are Alike](./solutions/1704-determine-if-string-halves-are-alike.js)|Easy|
1317+
1705|[Maximum Number of Eaten Apples](./solutions/1705-maximum-number-of-eaten-apples.js)|Medium|
13171318
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13181319
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13191320
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* 1705. Maximum Number of Eaten Apples
3+
* https://leetcode.com/problems/maximum-number-of-eaten-apples/
4+
* Difficulty: Medium
5+
*
6+
* There is a special kind of apple tree that grows apples every day for n days. On the ith day,
7+
* the tree grows apples[i] apples that will rot after days[i] days, that is on day i + days[i]
8+
* the apples will be rotten and cannot be eaten. On some days, the apple tree does not grow any
9+
* apples, which are denoted by apples[i] == 0 and days[i] == 0.
10+
*
11+
* You decided to eat at most one apple a day (to keep the doctors away). Note that you can keep
12+
* eating after the first n days.
13+
*
14+
* Given two integer arrays days and apples of length n, return the maximum number of apples you
15+
* can eat.
16+
*/
17+
18+
/**
19+
* @param {number[]} apples
20+
* @param {number[]} days
21+
* @return {number}
22+
*/
23+
var eatenApples = function(apples, days) {
24+
const expiryCounts = new Array(40001).fill(0);
25+
let result = 0;
26+
let earliestExpiry = Infinity;
27+
let maxExpiry = apples.length;
28+
29+
for (let day = 0; day <= maxExpiry; day++) {
30+
if (earliestExpiry < day) earliestExpiry = day;
31+
32+
if (day < apples.length && apples[day]) {
33+
const expiry = day + days[day] - 1;
34+
expiryCounts[expiry] += apples[day];
35+
earliestExpiry = Math.min(expiry, earliestExpiry);
36+
maxExpiry = Math.max(expiry, maxExpiry);
37+
}
38+
39+
while (!expiryCounts[earliestExpiry] && earliestExpiry < maxExpiry) {
40+
earliestExpiry++;
41+
}
42+
43+
if (expiryCounts[earliestExpiry]) {
44+
result++;
45+
expiryCounts[earliestExpiry]--;
46+
}
47+
}
48+
49+
return result;
50+
};

0 commit comments

Comments
 (0)