|
| 1 | +/** |
| 2 | + * [1553] Minimum Number of Days to Eat N Oranges |
| 3 | + * |
| 4 | + * There are n oranges in the kitchen and you decided to eat some of these oranges every day as follows: |
| 5 | + * |
| 6 | + * Eat one orange. |
| 7 | + * If the number of remaining oranges n is divisible by 2 then you can eat n / 2 oranges. |
| 8 | + * If the number of remaining oranges n is divisible by 3 then you can eat 2 * (n / 3) oranges. |
| 9 | + * |
| 10 | + * You can only choose one of the actions per day. |
| 11 | + * Given the integer n, return the minimum number of days to eat n oranges. |
| 12 | + * |
| 13 | + * Example 1: |
| 14 | + * |
| 15 | + * Input: n = 10 |
| 16 | + * Output: 4 |
| 17 | + * Explanation: You have 10 oranges. |
| 18 | + * Day 1: Eat 1 orange, 10 - 1 = 9. |
| 19 | + * Day 2: Eat 6 oranges, 9 - 2*(9/3) = 9 - 6 = 3. (Since 9 is divisible by 3) |
| 20 | + * Day 3: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1. |
| 21 | + * Day 4: Eat the last orange 1 - 1 = 0. |
| 22 | + * You need at least 4 days to eat the 10 oranges. |
| 23 | + * |
| 24 | + * Example 2: |
| 25 | + * |
| 26 | + * Input: n = 6 |
| 27 | + * Output: 3 |
| 28 | + * Explanation: You have 6 oranges. |
| 29 | + * Day 1: Eat 3 oranges, 6 - 6/2 = 6 - 3 = 3. (Since 6 is divisible by 2). |
| 30 | + * Day 2: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1. (Since 3 is divisible by 3) |
| 31 | + * Day 3: Eat the last orange 1 - 1 = 0. |
| 32 | + * You need at least 3 days to eat the 6 oranges. |
| 33 | + * |
| 34 | + * |
| 35 | + * Constraints: |
| 36 | + * |
| 37 | + * 1 <= n <= 2 * 10^9 |
| 38 | + * |
| 39 | + */ |
| 40 | +pub struct Solution {} |
| 41 | + |
| 42 | +// problem: https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/ |
| 43 | +// discuss: https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/discuss/?currentPage=1&orderBy=most_votes&query= |
| 44 | + |
| 45 | +// submission codes start here |
| 46 | + |
| 47 | +impl Solution { |
| 48 | + pub fn min_days(n: i32) -> i32 { |
| 49 | + let mut dp = std::collections::HashMap::new(); |
| 50 | + |
| 51 | + Self::dfs_helper(n, &mut dp) |
| 52 | + } |
| 53 | + |
| 54 | + fn dfs_helper(n: i32, dp: &mut std::collections::HashMap<i32, i32>) -> i32 { |
| 55 | + if n <= 1 { |
| 56 | + return n; |
| 57 | + } |
| 58 | + |
| 59 | + if !dp.contains_key(&n) { |
| 60 | + let a = n % 2 + Self::dfs_helper(n / 2, dp); |
| 61 | + let b = n % 3 + Self::dfs_helper(n / 3, dp); |
| 62 | + dp.insert(n, 1 + a.min(b)); |
| 63 | + } |
| 64 | + |
| 65 | + *dp.get(&n).unwrap() |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// submission codes end |
| 70 | + |
| 71 | +#[cfg(test)] |
| 72 | +mod tests { |
| 73 | + use super::*; |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn test_1553_example_1() { |
| 77 | + let n = 10; |
| 78 | + |
| 79 | + let result = 4; |
| 80 | + |
| 81 | + assert_eq!(Solution::min_days(n), result); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_1553_example_2() { |
| 86 | + let n = 6; |
| 87 | + |
| 88 | + let result = 3; |
| 89 | + |
| 90 | + assert_eq!(Solution::min_days(n), result); |
| 91 | + } |
| 92 | +} |
0 commit comments