From aa204dc1cef10cb2cef3e01eea9d4dfead1a6fc0 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 12 May 2024 09:19:43 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1553 No.1553.Minimum Number of Days to Eat N Oranges --- .../README.md | 44 ++++++++++++++++-- .../README_EN.md | 46 +++++++++++++++++-- .../Solution.cpp | 8 +++- .../Solution.py | 2 +- .../Solution.ts | 14 ++++++ 5 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.ts diff --git a/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/README.md b/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/README.md index 52b6b47438854..c2d8635c4c1f4 100644 --- a/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/README.md +++ b/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/README.md @@ -69,13 +69,30 @@ ### 方法一:记忆化搜索 +根据题目描述,对于每个 $n$,我们可以选择三种方式之一: + +1. 将 $n$ 减少 $1$; +2. 如果 $n$ 能被 $2$ 整除,将 $n$ 的值除以 $2$; +3. 如果 $n$ 能被 $3$ 整除,将 $n$ 的值除以 $3$。 + +因此,问题等价于求解通过上述三种方式,将 $n$ 减少到 $0$ 的最少天数。 + +我们设计一个函数 $dfs(n)$,表示将 $n$ 减少到 $0$ 的最少天数。函数 $dfs(n)$ 的执行过程如下: + +1. 如果 $n < 2$,返回 $n$; +2. 否则,我们可以先通过 $n \bmod 2$ 次操作 $1$,将 $n$ 减少到 $2$ 的倍数,然后执行操作 $2$,将 $n$ 减少到 $n/2$;我们也可以先通过 $n \bmod 3$ 次操作 $1$,将 $n$ 减少到 $3$ 的倍数,然后执行操作 $3$,将 $n$ 减少到 $n/3$。我们选择上述两种方式中最少的一种,即 $1 + \min(n \bmod 2 + dfs(n/2), n \bmod 3 + dfs(n/3))$。 + +为了避免重复计算,我们使用记忆化搜索的方法,将已经计算过的 $dfs(n)$ 的值存储在哈希表中。 + +时间复杂度 $O(\log^2 n)$,空间复杂度 $O(\log^2 n)$。 + ```python class Solution: def minDays(self, n: int) -> int: @cache - def dfs(n): + def dfs(n: int) -> int: if n < 2: return n return 1 + min(n % 2 + dfs(n // 2), n % 3 + dfs(n // 3)) @@ -115,8 +132,12 @@ public: } int dfs(int n) { - if (n < 2) return n; - if (f.count(n)) return f[n]; + if (n < 2) { + return n; + } + if (f.count(n)) { + return f[n]; + } int res = 1 + min(n % 2 + dfs(n / 2), n % 3 + dfs(n / 3)); f[n] = res; return res; @@ -140,6 +161,23 @@ func minDays(n int) int { } ``` +```ts +function minDays(n: number): number { + const f: Record = {}; + const dfs = (n: number): number => { + if (n < 2) { + return n; + } + if (f[n] !== undefined) { + return f[n]; + } + f[n] = 1 + Math.min((n % 2) + dfs((n / 2) | 0), (n % 3) + dfs((n / 3) | 0)); + return f[n]; + }; + return dfs(n); +} +``` + diff --git a/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/README_EN.md b/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/README_EN.md index 25e3e0752f116..3c8a73e3f323a 100644 --- a/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/README_EN.md +++ b/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/README_EN.md @@ -53,7 +53,24 @@ You need at least 3 days to eat the 6 oranges. ## Solutions -### Solution 1 +### Solution 1: Memoization Search + +According to the problem description, for each $n$, we can choose one of three ways: + +1. Decrease $n$ by $1$; +2. If $n$ can be divided by $2$, divide the value of $n$ by $2$; +3. If $n$ can be divided by $3$, divide the value of $n$ by $3$. + +Therefore, the problem is equivalent to finding the minimum number of days to reduce $n$ to $0$ through the above three ways. + +We design a function $dfs(n)$, which represents the minimum number of days to reduce $n$ to $0$. The execution process of the function $dfs(n)$ is as follows: + +1. If $n < 2$, return $n$; +2. Otherwise, we can first reduce $n$ to a multiple of $2$ by $n \bmod 2$ operations of $1$, and then perform operation $2$ to reduce $n$ to $n/2$; we can also first reduce $n$ to a multiple of $3$ by $n \bmod 3$ operations of $1$, and then perform operation $3$ to reduce $n$ to $n/3$. We choose the minimum of the above two ways, that is, $1 + \min(n \bmod 2 + dfs(n/2), n \bmod 3 + dfs(n/3))$. + +To avoid repeated calculations, we use the method of memoization search and store the calculated values of $dfs(n)$ in a hash table. + +The time complexity is $O(\log^2 n)$, and the space complexity is $O(\log^2 n)$. @@ -61,7 +78,7 @@ You need at least 3 days to eat the 6 oranges. class Solution: def minDays(self, n: int) -> int: @cache - def dfs(n): + def dfs(n: int) -> int: if n < 2: return n return 1 + min(n % 2 + dfs(n // 2), n % 3 + dfs(n // 3)) @@ -101,8 +118,12 @@ public: } int dfs(int n) { - if (n < 2) return n; - if (f.count(n)) return f[n]; + if (n < 2) { + return n; + } + if (f.count(n)) { + return f[n]; + } int res = 1 + min(n % 2 + dfs(n / 2), n % 3 + dfs(n / 3)); f[n] = res; return res; @@ -126,6 +147,23 @@ func minDays(n int) int { } ``` +```ts +function minDays(n: number): number { + const f: Record = {}; + const dfs = (n: number): number => { + if (n < 2) { + return n; + } + if (f[n] !== undefined) { + return f[n]; + } + f[n] = 1 + Math.min((n % 2) + dfs((n / 2) | 0), (n % 3) + dfs((n / 3) | 0)); + return f[n]; + }; + return dfs(n); +} +``` + diff --git a/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.cpp b/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.cpp index 8c0ad21210b20..00324e9d9b86f 100644 --- a/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.cpp +++ b/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.cpp @@ -7,8 +7,12 @@ class Solution { } int dfs(int n) { - if (n < 2) return n; - if (f.count(n)) return f[n]; + if (n < 2) { + return n; + } + if (f.count(n)) { + return f[n]; + } int res = 1 + min(n % 2 + dfs(n / 2), n % 3 + dfs(n / 3)); f[n] = res; return res; diff --git a/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.py b/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.py index 695d078f05a75..6b9f6e3a06a60 100644 --- a/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.py +++ b/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.py @@ -1,7 +1,7 @@ class Solution: def minDays(self, n: int) -> int: @cache - def dfs(n): + def dfs(n: int) -> int: if n < 2: return n return 1 + min(n % 2 + dfs(n // 2), n % 3 + dfs(n // 3)) diff --git a/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.ts b/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.ts new file mode 100644 index 0000000000000..6c25c39238955 --- /dev/null +++ b/solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.ts @@ -0,0 +1,14 @@ +function minDays(n: number): number { + const f: Record = {}; + const dfs = (n: number): number => { + if (n < 2) { + return n; + } + if (f[n] !== undefined) { + return f[n]; + } + f[n] = 1 + Math.min((n % 2) + dfs((n / 2) | 0), (n % 3) + dfs((n / 3) | 0)); + return f[n]; + }; + return dfs(n); +}