File tree 2 files changed +37
-1
lines changed
2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,363 LeetCode solutions in JavaScript
1
+ # 1,364 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1186
1186
1550|[ Three Consecutive Odds] ( ./solutions/1550-three-consecutive-odds.js ) |Easy|
1187
1187
1551|[ Minimum Operations to Make Array Equal] ( ./solutions/1551-minimum-operations-to-make-array-equal.js ) |Medium|
1188
1188
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|
1189
1190
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|
1190
1191
1576|[ Replace All ?'s to Avoid Consecutive Repeating Characters] ( ./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js ) |Medium|
1191
1192
1598|[ Crawler Log Folder] ( ./solutions/1598-crawler-log-folder.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments