Skip to content

Commit a6eedae

Browse files
committed
Add solution #1553
1 parent 37f9421 commit a6eedae

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,363 LeetCode solutions in JavaScript
1+
# 1,364 LeetCode solutions in JavaScript
22

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

@@ -1186,6 +1186,7 @@
11861186
1550|[Three Consecutive Odds](./solutions/1550-three-consecutive-odds.js)|Easy|
11871187
1551|[Minimum Operations to Make Array Equal](./solutions/1551-minimum-operations-to-make-array-equal.js)|Medium|
11881188
1552|[Magnetic Force Between Two Balls](./solutions/1552-magnetic-force-between-two-balls.js)|Medium|
1189+
1553|[Minimum Number of Days to Eat N Oranges](./solutions/1553-minimum-number-of-days-to-eat-n-oranges.js)|Hard|
11891190
1566|[Detect Pattern of Length M Repeated K or More Times](./solutions/1566-detect-pattern-of-length-m-repeated-k-or-more-times.js)|Easy|
11901191
1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js)|Medium|
11911192
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 1553. Minimum Number of Days to Eat N Oranges
3+
* https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/
4+
* Difficulty: Hard
5+
*
6+
* There are n oranges in the kitchen and you decided to eat some of these oranges every day as
7+
* follows:
8+
* - Eat one orange.
9+
* - If the number of remaining oranges n is divisible by 2 then you can eat n / 2 oranges.
10+
* - If the number of remaining oranges n is divisible by 3 then you can eat 2 * (n / 3) oranges.
11+
*
12+
* You can only choose one of the actions per day.
13+
*
14+
* Given the integer n, return the minimum number of days to eat n oranges.
15+
*/
16+
17+
/**
18+
* @param {number} n
19+
* @return {number}
20+
*/
21+
var minDays = function(n) {
22+
const memo = new Map();
23+
return computeMinDays(n);
24+
25+
function computeMinDays(oranges) {
26+
if (oranges <= 1) return oranges;
27+
if (memo.has(oranges)) return memo.get(oranges);
28+
29+
const eatHalf = computeMinDays(Math.floor(oranges / 2)) + (oranges % 2) + 1;
30+
const eatTwoThirds = computeMinDays(Math.floor(oranges / 3)) + (oranges % 3) + 1;
31+
const minDays = Math.min(eatHalf, eatTwoThirds);
32+
memo.set(oranges, minDays);
33+
return minDays;
34+
}
35+
};

0 commit comments

Comments
 (0)