From 0672aaeb0ecf6e798d4111e5d3746415624383b3 Mon Sep 17 00:00:00 2001 From: rain84 Date: Wed, 10 Jul 2024 21:25:26 +0300 Subject: [PATCH 01/28] feat: add ts solution to lc problem: No.0746 --- .../0746.Min Cost Climbing Stairs/README.md | 30 +++++++++++++++++++ .../README_EN.md | 30 +++++++++++++++++++ .../Solution3.ts | 15 ++++++++++ 3 files changed, 75 insertions(+) create mode 100644 solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.ts diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md index 26266f0960f81..a3b820235942c 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md @@ -266,4 +266,34 @@ impl Solution { + + +### Solution 3: Dynamic Programming. Recursion and top-down approach + + + +#### TypeScript + +```ts +function minCostClimbingStairs(cost: number[]): number { + const n = cost.length; + const cache = Array(n).fill(-1); + + const fn = (i: number): number => { + if (i <= 1) return cost[i]; + if (cache[i] !== undefined && cache[i] !== -1) return cache[i]; + + cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2)); + + return cache[i]; + }; + + return fn(n); +} +``` + + + + + diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md b/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md index fb3ea3a8a0f35..42d781975b2db 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md @@ -264,4 +264,34 @@ impl Solution { + + +### Solution 3: Dynamic Programming. Recursion and top-down approach + + + +#### TypeScript + +```ts +function minCostClimbingStairs(cost: number[]): number { + const n = cost.length; + const cache = Array(n).fill(-1); + + const fn = (i: number): number => { + if (i <= 1) return cost[i]; + if (cache[i] !== undefined && cache[i] !== -1) return cache[i]; + + cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2)); + + return cache[i]; + }; + + return fn(n); +} +``` + + + + + diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.ts b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.ts new file mode 100644 index 0000000000000..bced8b2a41393 --- /dev/null +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.ts @@ -0,0 +1,15 @@ +function minCostClimbingStairs(cost: number[]): number { + const n = cost.length; + const cache = Array(n).fill(-1); + + const fn = (i: number): number => { + if (i <= 1) return cost[i]; + if (cache[i] !== undefined && cache[i] !== -1) return cache[i]; + + cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2)); + + return cache[i]; + }; + + return fn(n); +} From 7a53e2a8388c0158168ac6e6a67089e36c398ad2 Mon Sep 17 00:00:00 2001 From: rain84 Date: Wed, 10 Jul 2024 21:27:10 +0300 Subject: [PATCH 02/28] feat: add js solution to lc problem: No.0746 --- .../0746.Min Cost Climbing Stairs/README.md | 20 +++++++++++++++++++ .../README_EN.md | 20 +++++++++++++++++++ .../Solution3.js | 15 ++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.js diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md index a3b820235942c..d00715564551b 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md @@ -292,6 +292,26 @@ function minCostClimbingStairs(cost: number[]): number { } ``` +#### JavaScript + +```js +function minCostClimbingStairs(cost) { + const n = cost.length; + const cache = Array(n).fill(-1); + + const fn = i => { + if (i <= 1) return cost[i]; + if (cache[i] !== undefined && cache[i] !== -1) return cache[i]; + + cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2)); + + return cache[i]; + }; + + return fn(n); +} +``` + diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md b/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md index 42d781975b2db..2e7c5f563662b 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md @@ -290,6 +290,26 @@ function minCostClimbingStairs(cost: number[]): number { } ``` +#### JavaScript + +```js +function minCostClimbingStairs(cost) { + const n = cost.length; + const cache = Array(n).fill(-1); + + const fn = i => { + if (i <= 1) return cost[i]; + if (cache[i] !== undefined && cache[i] !== -1) return cache[i]; + + cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2)); + + return cache[i]; + }; + + return fn(n); +} +``` + diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.js b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.js new file mode 100644 index 0000000000000..5b0a7cde9f2c1 --- /dev/null +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.js @@ -0,0 +1,15 @@ +function minCostClimbingStairs(cost) { + const n = cost.length; + const cache = Array(n).fill(-1); + + const fn = i => { + if (i <= 1) return cost[i]; + if (cache[i] !== undefined && cache[i] !== -1) return cache[i]; + + cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2)); + + return cache[i]; + }; + + return fn(n); +} From cd8966afb92186fce0ddbdb1ada6203ebaf344e2 Mon Sep 17 00:00:00 2001 From: rain84 Date: Wed, 10 Jul 2024 21:29:11 +0300 Subject: [PATCH 03/28] feat: add js solution to lc problem: No.0746 --- .../0746.Min Cost Climbing Stairs/README.md | 13 +++++++++++++ .../0746.Min Cost Climbing Stairs/README_EN.md | 13 +++++++++++++ .../0746.Min Cost Climbing Stairs/Solution2.js | 8 ++++++++ 3 files changed, 34 insertions(+) create mode 100644 solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.js diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md index d00715564551b..04f618d23f6af 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md @@ -246,6 +246,19 @@ function minCostClimbingStairs(cost: number[]): number { } ``` +#### JavaScript + +```js +function minCostClimbingStairs(cost) { + let a = 0, + b = 0; + for (let i = 1; i < cost.length; ++i) { + [a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])]; + } + return b; +} +``` + #### Rust ```rust diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md b/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md index 2e7c5f563662b..bbd14b47b81d5 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md @@ -244,6 +244,19 @@ function minCostClimbingStairs(cost: number[]): number { } ``` +#### JavaScript + +```js +function minCostClimbingStairs(cost) { + let a = 0, + b = 0; + for (let i = 1; i < cost.length; ++i) { + [a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])]; + } + return b; +} +``` + #### Rust ```rust diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.js b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.js new file mode 100644 index 0000000000000..dd7c09310a9cd --- /dev/null +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.js @@ -0,0 +1,8 @@ +function minCostClimbingStairs(cost) { + let a = 0, + b = 0; + for (let i = 1; i < cost.length; ++i) { + [a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])]; + } + return b; +} From f9ef4145c696647c8069ef31361f05bf8ea89d59 Mon Sep 17 00:00:00 2001 From: rain84 Date: Wed, 10 Jul 2024 21:30:31 +0300 Subject: [PATCH 04/28] feat: add js solution to lc problem: No.0746 --- .../0746.Min Cost Climbing Stairs/README.md | 13 +++++++++++++ .../0746.Min Cost Climbing Stairs/README_EN.md | 13 +++++++++++++ .../0746.Min Cost Climbing Stairs/Solution.js | 8 ++++++++ 3 files changed, 34 insertions(+) create mode 100644 solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.js diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md index 04f618d23f6af..14b3de01e601f 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md @@ -152,6 +152,19 @@ function minCostClimbingStairs(cost: number[]): number { } ``` +#### JavaScript + +```js +function minCostClimbingStairs(cost) { + const n = cost.length; + const f = Array(n + 1).fill(0); + for (let i = 2; i <= n; ++i) { + f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); + } + return f[n]; +} +``` + #### Rust ```rust diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md b/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md index bbd14b47b81d5..1e3973e532545 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md @@ -150,6 +150,19 @@ function minCostClimbingStairs(cost: number[]): number { } ``` +#### JavaScript + +```js +function minCostClimbingStairs(cost) { + const n = cost.length; + const f = Array(n + 1).fill(0); + for (let i = 2; i <= n; ++i) { + f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); + } + return f[n]; +} +``` + #### Rust ```rust diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.js b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.js new file mode 100644 index 0000000000000..42ed843b3b032 --- /dev/null +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.js @@ -0,0 +1,8 @@ +function minCostClimbingStairs(cost) { + const n = cost.length; + const f = Array(n + 1).fill(0); + for (let i = 2; i <= n; ++i) { + f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); + } + return f[n]; +} From f32bb1365a0c35da900659dbcabca3763ed6d095 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:42:34 +0800 Subject: [PATCH 05/28] Update README.md --- .../0746.Min Cost Climbing Stairs/README.md | 274 +++++++++++++----- 1 file changed, 196 insertions(+), 78 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md index 14b3de01e601f..d2162dec5227c 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md @@ -65,7 +65,177 @@ tags: -### 方法一:动态规划 +### 方法一:记忆化搜索 + +我们设计一个函数 $textit{dfs}(i)$,表示从第 $i$ 个阶梯开始爬楼梯所需要的最小花费。那么答案为 $\min(\textit{dfs}(0), \textit{dfs}(1))$。 + +函数 $\textit{dfs}(i)$ 的执行过程如下: + +- 如果 $i \ge \textit{len(cost)}$,表示当前位置已经超过了楼梯顶部,不需要再爬楼梯,返回 $0$; +- 否则,我们可以选择爬 $1$ 级楼梯,花费为 $\textit{cost}[i]$,然后递归调用 $\textit{dfs}(i + 1)$;也可以选择爬 $2$ 级楼梯,花费为 $\textit{cost}[i]$,然后递归调用 $\textit{dfs}(i + 2)$; +- 返回两种方案中的最小花费。 + +为了避免重复计算,我们使用记忆化搜索的方法,将已经计算过的结果保存在数组或哈希表中。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{cost}$ 的长度。 + + + +#### Python3 + +```python +class Solution: + def minCostClimbingStairs(self, cost: List[int]) -> int: + @cache + def dfs(i: int) -> int: + if i >= len(cost): + return 0 + return cost[i] + min(dfs(i + 1), dfs(i + 2)) + + return min(dfs(0), dfs(1)) +``` + +#### Java + +```java +class Solution { + private Integer[] f; + private int[] cost; + + public int minCostClimbingStairs(int[] cost) { + this.cost = cost; + f = new Integer[cost.length]; + return Math.min(dfs(0), dfs(1)); + } + + private int dfs(int i) { + if (i >= cost.length) { + return 0; + } + if (f[i] == null) { + f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2)); + } + return f[i]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int minCostClimbingStairs(vector& cost) { + int n = cost.size(); + int f[n]; + memset(f, -1, sizeof(f)); + auto dfs = [&](auto&& dfs, int i) -> int { + if (i >= n) { + return 0; + } + if (f[i] < 0) { + f[i] = cost[i] + min(dfs(dfs, i + 1), dfs(dfs, i + 2)); + } + return f[i]; + }; + return min(dfs(dfs, 0), dfs(dfs, 1)); + } +}; +``` + +#### Go + +```go +func minCostClimbingStairs(cost []int) int { + n := len(cost) + f := make([]int, n) + for i := range f { + f[i] = -1 + } + var dfs func(int) int + dfs = func(i int) int { + if i >= n { + return 0 + } + if f[i] < 0 { + f[i] = cost[i] + min(dfs(i+1), dfs(i+2)) + } + return f[i] + } + return min(dfs(0), dfs(1)) +} +``` + +#### TypeScript + +```ts +function minCostClimbingStairs(cost: number[]): number { + const n = cost.length; + const f: number[] = Array(n).fill(-1); + const dfs = (i: number): number => { + if (i >= n) { + return 0; + } + if (f[i] < 0) { + f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2)); + } + return f[i]; + }; + return Math.min(dfs(0), dfs(1)); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { + let n = cost.len(); + let mut f = vec![-1; n]; + + fn dfs(i: usize, cost: &Vec, f: &mut Vec, n: usize) -> i32 { + if i >= n { + return 0; + } + if f[i] < 0 { + let next1 = dfs(i + 1, cost, f, n); + let next2 = dfs(i + 2, cost, f, n); + f[i] = cost[i] + next1.min(next2); + } + f[i] + } + + dfs(0, &cost, &mut f, n).min(dfs(1, &cost, &mut f, n)) + } +} +``` + +#### JavaScript + +```js +function minCostClimbingStairs(cost) { + const n = cost.length; + const f = Array(n).fill(-1); + const dfs = i => { + if (i >= n) { + return 0; + } + if (f[i] < 0) { + f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2)); + } + return f[i]; + }; + return Math.min(dfs(0), dfs(1)); +} +``` + + + + + + + +### 方法二:动态规划 我们定义 $f[i]$ 表示到达第 $i$ 个阶梯所需要的最小花费,初始时 $f[0] = f[1] = 0$,答案即为 $f[n]$。 @@ -77,9 +247,7 @@ $$ 最终的答案即为 $f[n]$。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `cost` 的长度。 - -我们注意到,状态转移方程中的 $f[i]$ 只和 $f[i - 1]$ 与 $f[i - 2]$ 有关,因此我们可以使用两个变量 $f$ 和 $g$ 交替地记录 $f[i - 2]$ 和 $f[i - 1]$ 的值,这样空间复杂度可以优化到 $O(1)$。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{cost}$ 的长度。 @@ -152,19 +320,6 @@ function minCostClimbingStairs(cost: number[]): number { } ``` -#### JavaScript - -```js -function minCostClimbingStairs(cost) { - const n = cost.length; - const f = Array(n + 1).fill(0); - for (let i = 2; i <= n; ++i) { - f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); - } - return f[n]; -} -``` - #### Rust ```rust @@ -180,13 +335,28 @@ impl Solution { } ``` +#### JavaScript + +```js +function minCostClimbingStairs(cost) { + const n = cost.length; + const f = Array(n + 1).fill(0); + for (let i = 2; i <= n; ++i) { + f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); + } + return f[n]; +} +``` + -### 方法二 +### 方法三:动态规划(空间优化) + +我们注意到,状态转移方程中的 $f[i]$ 只和 $f[i - 1]$ 与 $f[i - 2]$ 有关,因此我们可以使用两个变量 $f$ 和 $g$ 交替地记录 $f[i - 2]$ 和 $f[i - 1]$ 的值,这样空间复杂度可以优化到 $O(1)$。 @@ -250,25 +420,11 @@ func minCostClimbingStairs(cost []int) int { ```ts function minCostClimbingStairs(cost: number[]): number { - let a = 0, - b = 0; + let [f, g] = [0, 0]; for (let i = 1; i < cost.length; ++i) { - [a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])]; + [f, g] = [g, Math.min(f + cost[i - 1], g + cost[i])]; } - return b; -} -``` - -#### JavaScript - -```js -function minCostClimbingStairs(cost) { - let a = 0, - b = 0; - for (let i = 1; i < cost.length; ++i) { - [a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])]; - } - return b; + return g; } ``` @@ -288,53 +444,15 @@ impl Solution { } ``` - - - - - - -### Solution 3: Dynamic Programming. Recursion and top-down approach - - - -#### TypeScript - -```ts -function minCostClimbingStairs(cost: number[]): number { - const n = cost.length; - const cache = Array(n).fill(-1); - - const fn = (i: number): number => { - if (i <= 1) return cost[i]; - if (cache[i] !== undefined && cache[i] !== -1) return cache[i]; - - cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2)); - - return cache[i]; - }; - - return fn(n); -} -``` - #### JavaScript ```js function minCostClimbingStairs(cost) { - const n = cost.length; - const cache = Array(n).fill(-1); - - const fn = i => { - if (i <= 1) return cost[i]; - if (cache[i] !== undefined && cache[i] !== -1) return cache[i]; - - cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2)); - - return cache[i]; - }; - - return fn(n); + let [f, g] = [0, 0]; + for (let i = 1; i < cost.length; ++i) { + [f, g] = [g, Math.min(f + cost[i - 1], g + cost[i])]; + } + return g; } ``` From 024f3577b3d07018dcf38692a8d9a5e56db3e241 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:42:58 +0800 Subject: [PATCH 06/28] Update README_EN.md --- .../README_EN.md | 280 +++++++++++++----- 1 file changed, 199 insertions(+), 81 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md b/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md index 1e3973e532545..4708c37382cdb 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/README_EN.md @@ -63,21 +63,189 @@ The total cost is 6. -### Solution 1: Dynamic Programming +### Solution 1: Memoization Search -We define $f[i]$ as the minimum cost required to reach the $i$th step, initially $f[0] = f[1] = 0$. The answer is $f[n]$. +We design a function $\textit{dfs}(i)$, which represents the minimum cost required to climb the stairs starting from the $i$-th step. Therefore, the answer is $\min(\textit{dfs}(0), \textit{dfs}(1))$. -When $i \ge 2$, we can directly reach the $i$th step from the $(i - 1)$th step using $1$ step, or reach the $i$th step from the $(i - 2)$th step using $2$ steps. Therefore, we have the state transition equation: +The execution process of the function $\textit{dfs}(i)$ is as follows: + +- If $i \ge \textit{len(cost)}$, it means the current position has exceeded the top of the stairs, and there is no need to climb further, so return $0$; +- Otherwise, we can choose to climb $1$ step with a cost of $\textit{cost}[i]$, then recursively call $\textit{dfs}(i + 1)$; or we can choose to climb $2$ steps with a cost of $\textit{cost}[i]$, then recursively call $\textit{dfs}(i + 2)$; +- Return the minimum cost between these two options. + +To avoid repeated calculations, we use memoization search, saving the results that have already been calculated in an array or hash table. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{cost}$. + + + +#### Python3 + +```python +class Solution: + def minCostClimbingStairs(self, cost: List[int]) -> int: + @cache + def dfs(i: int) -> int: + if i >= len(cost): + return 0 + return cost[i] + min(dfs(i + 1), dfs(i + 2)) + + return min(dfs(0), dfs(1)) +``` + +#### Java + +```java +class Solution { + private Integer[] f; + private int[] cost; + + public int minCostClimbingStairs(int[] cost) { + this.cost = cost; + f = new Integer[cost.length]; + return Math.min(dfs(0), dfs(1)); + } + + private int dfs(int i) { + if (i >= cost.length) { + return 0; + } + if (f[i] == null) { + f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2)); + } + return f[i]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int minCostClimbingStairs(vector& cost) { + int n = cost.size(); + int f[n]; + memset(f, -1, sizeof(f)); + auto dfs = [&](auto&& dfs, int i) -> int { + if (i >= n) { + return 0; + } + if (f[i] < 0) { + f[i] = cost[i] + min(dfs(dfs, i + 1), dfs(dfs, i + 2)); + } + return f[i]; + }; + return min(dfs(dfs, 0), dfs(dfs, 1)); + } +}; +``` + +#### Go + +```go +func minCostClimbingStairs(cost []int) int { + n := len(cost) + f := make([]int, n) + for i := range f { + f[i] = -1 + } + var dfs func(int) int + dfs = func(i int) int { + if i >= n { + return 0 + } + if f[i] < 0 { + f[i] = cost[i] + min(dfs(i+1), dfs(i+2)) + } + return f[i] + } + return min(dfs(0), dfs(1)) +} +``` + +#### TypeScript + +```ts +function minCostClimbingStairs(cost: number[]): number { + const n = cost.length; + const f: number[] = Array(n).fill(-1); + const dfs = (i: number): number => { + if (i >= n) { + return 0; + } + if (f[i] < 0) { + f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2)); + } + return f[i]; + }; + return Math.min(dfs(0), dfs(1)); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { + let n = cost.len(); + let mut f = vec![-1; n]; + + fn dfs(i: usize, cost: &Vec, f: &mut Vec, n: usize) -> i32 { + if i >= n { + return 0; + } + if f[i] < 0 { + let next1 = dfs(i + 1, cost, f, n); + let next2 = dfs(i + 2, cost, f, n); + f[i] = cost[i] + next1.min(next2); + } + f[i] + } + + dfs(0, &cost, &mut f, n).min(dfs(1, &cost, &mut f, n)) + } +} +``` + +#### JavaScript + +```js +function minCostClimbingStairs(cost) { + const n = cost.length; + const f = Array(n).fill(-1); + const dfs = i => { + if (i >= n) { + return 0; + } + if (f[i] < 0) { + f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2)); + } + return f[i]; + }; + return Math.min(dfs(0), dfs(1)); +} +``` + + + + + + + +### Solution 2: Dynamic Programming + +We define $f[i]$ as the minimum cost needed to reach the $i$-th stair. Initially, $f[0] = f[1] = 0$, and the answer is $f[n]$. + +When $i \ge 2$, we can reach the $i$-th stair directly from the $(i - 1)$-th stair with one step, or from the $(i - 2)$-th stair with two steps. Therefore, we have the state transition equation: $$ -f[i] = \min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]) +f[i] = \min(f[i - 1] + \textit{cost}[i - 1], f[i - 2] + \textit{cost}[i - 2]) $$ The final answer is $f[n]$. -The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the `cost` array. - -We notice that $f[i]$ in the state transition equation is only related to $f[i - 1]$ and $f[i - 2]$. Therefore, we can use two variables $f$ and $g$ to alternately record the values of $f[i - 2]$ and $f[i - 1]$, which optimizes the space complexity to $O(1)$. +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{cost}$. @@ -150,19 +318,6 @@ function minCostClimbingStairs(cost: number[]): number { } ``` -#### JavaScript - -```js -function minCostClimbingStairs(cost) { - const n = cost.length; - const f = Array(n + 1).fill(0); - for (let i = 2; i <= n; ++i) { - f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); - } - return f[n]; -} -``` - #### Rust ```rust @@ -178,13 +333,28 @@ impl Solution { } ``` +#### JavaScript + +```js +function minCostClimbingStairs(cost) { + const n = cost.length; + const f = Array(n + 1).fill(0); + for (let i = 2; i <= n; ++i) { + f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); + } + return f[n]; +} +``` + -### Solution 2 +### Solution 3: Dynamic Programming (Space Optimization) + +We notice that the state transition equation for $f[i]$ only depends on $f[i - 1]$ and $f[i - 2]$. Therefore, we can use two variables $f$ and $g$ to alternately record the values of $f[i - 2]$ and $f[i - 1]$, thus optimizing the space complexity to $O(1)$. @@ -248,25 +418,11 @@ func minCostClimbingStairs(cost []int) int { ```ts function minCostClimbingStairs(cost: number[]): number { - let a = 0, - b = 0; + let [f, g] = [0, 0]; for (let i = 1; i < cost.length; ++i) { - [a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])]; + [f, g] = [g, Math.min(f + cost[i - 1], g + cost[i])]; } - return b; -} -``` - -#### JavaScript - -```js -function minCostClimbingStairs(cost) { - let a = 0, - b = 0; - for (let i = 1; i < cost.length; ++i) { - [a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])]; - } - return b; + return g; } ``` @@ -286,53 +442,15 @@ impl Solution { } ``` - - - - - - -### Solution 3: Dynamic Programming. Recursion and top-down approach - - - -#### TypeScript - -```ts -function minCostClimbingStairs(cost: number[]): number { - const n = cost.length; - const cache = Array(n).fill(-1); - - const fn = (i: number): number => { - if (i <= 1) return cost[i]; - if (cache[i] !== undefined && cache[i] !== -1) return cache[i]; - - cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2)); - - return cache[i]; - }; - - return fn(n); -} -``` - #### JavaScript ```js function minCostClimbingStairs(cost) { - const n = cost.length; - const cache = Array(n).fill(-1); - - const fn = i => { - if (i <= 1) return cost[i]; - if (cache[i] !== undefined && cache[i] !== -1) return cache[i]; - - cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2)); - - return cache[i]; - }; - - return fn(n); + let [f, g] = [0, 0]; + for (let i = 1; i < cost.length; ++i) { + [f, g] = [g, Math.min(f + cost[i - 1], g + cost[i])]; + } + return g; } ``` From b83575fef67303b8ea85966acb33bac77bd7adda Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:43:26 +0800 Subject: [PATCH 07/28] Update Solution.cpp --- .../Solution.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.cpp b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.cpp index 16a0e8f681057..a3b6b0cfe14c5 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.cpp +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.cpp @@ -2,10 +2,17 @@ class Solution { public: int minCostClimbingStairs(vector& cost) { int n = cost.size(); - vector f(n + 1); - for (int i = 2; i <= n; ++i) { - f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]); - } - return f[n]; + int f[n]; + memset(f, -1, sizeof(f)); + auto dfs = [&](auto&& dfs, int i) -> int { + if (i >= n) { + return 0; + } + if (f[i] < 0) { + f[i] = cost[i] + min(dfs(dfs, i + 1), dfs(dfs, i + 2)); + } + return f[i]; + }; + return min(dfs(dfs, 0), dfs(dfs, 1)); } -}; \ No newline at end of file +}; From 1541fd344305feced1979c3f29d3c72fccc8f6fe Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:43:35 +0800 Subject: [PATCH 08/28] Update Solution.go --- .../0746.Min Cost Climbing Stairs/Solution.go | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.go b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.go index 97ce4e397a98e..793e6f9639c2f 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.go +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.go @@ -1,8 +1,18 @@ func minCostClimbingStairs(cost []int) int { n := len(cost) - f := make([]int, n+1) - for i := 2; i <= n; i++ { - f[i] = min(f[i-1]+cost[i-1], f[i-2]+cost[i-2]) + f := make([]int, n) + for i := range f { + f[i] = -1 } - return f[n] -} \ No newline at end of file + var dfs func(int) int + dfs = func(i int) int { + if i >= n { + return 0 + } + if f[i] < 0 { + f[i] = cost[i] + min(dfs(i+1), dfs(i+2)) + } + return f[i] + } + return min(dfs(0), dfs(1)) +} From 45a9bd081a2e7f62fd9cdf471422e75c3cbca100 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:43:42 +0800 Subject: [PATCH 09/28] Update Solution.java --- .../Solution.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.java b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.java index c69c7d7398fe7..071b02cee1448 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.java +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.java @@ -1,10 +1,20 @@ class Solution { + private Integer[] f; + private int[] cost; + public int minCostClimbingStairs(int[] cost) { - int n = cost.length; - int[] f = new int[n + 1]; - for (int i = 2; i <= n; ++i) { - f[i] = Math.min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]); + this.cost = cost; + f = new Integer[cost.length]; + return Math.min(dfs(0), dfs(1)); + } + + private int dfs(int i) { + if (i >= cost.length) { + return 0; + } + if (f[i] == null) { + f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2)); } - return f[n]; + return f[i]; } -} \ No newline at end of file +} From dc7692a14a3c8b09f9b22e99c0ec6b5718e6b8d9 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:43:52 +0800 Subject: [PATCH 10/28] Update Solution.js --- .../0746.Min Cost Climbing Stairs/Solution.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.js b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.js index 42ed843b3b032..5d2dd4be53d22 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.js +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.js @@ -1,8 +1,14 @@ function minCostClimbingStairs(cost) { const n = cost.length; - const f = Array(n + 1).fill(0); - for (let i = 2; i <= n; ++i) { - f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); - } - return f[n]; + const f = Array(n).fill(-1); + const dfs = i => { + if (i >= n) { + return 0; + } + if (f[i] < 0) { + f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2)); + } + return f[i]; + }; + return Math.min(dfs(0), dfs(1)); } From 486052e7bde55e0ac50fbd2004a280e40d3dc6ac Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:43:59 +0800 Subject: [PATCH 11/28] Update Solution.py --- .../0746.Min Cost Climbing Stairs/Solution.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.py b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.py index 3c29d129b947b..6c7903ff4688c 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.py +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.py @@ -1,7 +1,9 @@ class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: - n = len(cost) - f = [0] * (n + 1) - for i in range(2, n + 1): - f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]) - return f[n] + @cache + def dfs(i: int) -> int: + if i >= len(cost): + return 0 + return cost[i] + min(dfs(i + 1), dfs(i + 2)) + + return min(dfs(0), dfs(1)) From 62fac6cce85e5c6215d665e572a22a9a0a6612b1 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:44:08 +0800 Subject: [PATCH 12/28] Update Solution.rs --- .../0746.Min Cost Climbing Stairs/Solution.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.rs b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.rs index 82eb36fc1ec4b..50c79eebdedb3 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.rs +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.rs @@ -1,10 +1,20 @@ impl Solution { pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { let n = cost.len(); - let mut f = vec![0; n + 1]; - for i in 2..=n { - f[i] = std::cmp::min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]); + let mut f = vec![-1; n]; + + fn dfs(i: usize, cost: &Vec, f: &mut Vec, n: usize) -> i32 { + if i >= n { + return 0; + } + if f[i] < 0 { + let next1 = dfs(i + 1, cost, f, n); + let next2 = dfs(i + 2, cost, f, n); + f[i] = cost[i] + next1.min(next2); + } + f[i] } - f[n] + + dfs(0, &cost, &mut f, n).min(dfs(1, &cost, &mut f, n)) } } From 14e167d3386af1e8fecdba596923f15425170b40 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:44:16 +0800 Subject: [PATCH 13/28] Update Solution.ts --- .../0746.Min Cost Climbing Stairs/Solution.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.ts b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.ts index 7c51ae65fec4f..c152e5ed5b0e7 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.ts +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution.ts @@ -1,8 +1,14 @@ function minCostClimbingStairs(cost: number[]): number { const n = cost.length; - const f: number[] = Array(n + 1).fill(0); - for (let i = 2; i <= n; ++i) { - f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); - } - return f[n]; + const f: number[] = Array(n).fill(-1); + const dfs = (i: number): number => { + if (i >= n) { + return 0; + } + if (f[i] < 0) { + f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2)); + } + return f[i]; + }; + return Math.min(dfs(0), dfs(1)); } From d0276ca6b5b077c9e66544ea2801341214cbbec3 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:44:25 +0800 Subject: [PATCH 14/28] Update Solution2.cpp --- .../0746.Min Cost Climbing Stairs/Solution2.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.cpp b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.cpp index 8add5acdee316..abf7189376b10 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.cpp +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.cpp @@ -1,12 +1,11 @@ class Solution { public: int minCostClimbingStairs(vector& cost) { - int f = 0, g = 0; - for (int i = 2; i <= cost.size(); ++i) { - int gg = min(f + cost[i - 2], g + cost[i - 1]); - f = g; - g = gg; + int n = cost.size(); + vector f(n + 1); + for (int i = 2; i <= n; ++i) { + f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]); } - return g; + return f[n]; } -}; \ No newline at end of file +}; From 8d06a9100bd3f3adca885db3e2e1dd040deab9e3 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:44:42 +0800 Subject: [PATCH 15/28] Update Solution2.go --- .../0700-0799/0746.Min Cost Climbing Stairs/Solution2.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.go b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.go index 24764a4c60f57..4bad897627512 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.go +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.go @@ -1,7 +1,8 @@ func minCostClimbingStairs(cost []int) int { - var f, g int + n := len(cost) + f := make([]int, n+1) for i := 2; i <= n; i++ { - f, g = g, min(f+cost[i-2], g+cost[i-1]) + f[i] = min(f[i-1]+cost[i-1], f[i-2]+cost[i-2]) } - return g -} \ No newline at end of file + return f[n] +} From 4b57ba1c8e3e4ab8f4e10888619791f680d017f1 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:44:55 +0800 Subject: [PATCH 16/28] Update Solution2.java --- .../0746.Min Cost Climbing Stairs/Solution2.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.java b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.java index 87e58dc41de5d..f703f0e0efd2f 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.java +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.java @@ -1,11 +1,10 @@ class Solution { public int minCostClimbingStairs(int[] cost) { - int f = 0, g = 0; - for (int i = 2; i <= cost.length; ++i) { - int gg = Math.min(f + cost[i - 2], g + cost[i - 1]); - f = g; - g = gg; + int n = cost.length; + int[] f = new int[n + 1]; + for (int i = 2; i <= n; ++i) { + f[i] = Math.min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]); } - return g; + return f[n]; } -} \ No newline at end of file +} From e7760bad5c1fdfba22991cd0ea064f2c51f1ee52 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:45:03 +0800 Subject: [PATCH 17/28] Update Solution2.js --- .../0746.Min Cost Climbing Stairs/Solution2.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.js b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.js index dd7c09310a9cd..42ed843b3b032 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.js +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.js @@ -1,8 +1,8 @@ function minCostClimbingStairs(cost) { - let a = 0, - b = 0; - for (let i = 1; i < cost.length; ++i) { - [a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])]; + const n = cost.length; + const f = Array(n + 1).fill(0); + for (let i = 2; i <= n; ++i) { + f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); } - return b; + return f[n]; } From 67559a249712c68b5e83ef32e808534dfa6af826 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:45:15 +0800 Subject: [PATCH 18/28] Update Solution2.py --- .../0700-0799/0746.Min Cost Climbing Stairs/Solution2.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.py b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.py index bc756e0b55d92..3c29d129b947b 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.py +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.py @@ -1,6 +1,7 @@ class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: - f = g = 0 - for i in range(2, len(cost) + 1): - f, g = g, min(f + cost[i - 2], g + cost[i - 1]) - return g + n = len(cost) + f = [0] * (n + 1) + for i in range(2, n + 1): + f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]) + return f[n] From ec5200080289ff82b2e4826489601031ed40ca07 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:46:04 +0800 Subject: [PATCH 19/28] Update Solution2.rs --- .../0746.Min Cost Climbing Stairs/Solution2.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.rs b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.rs index 5d62ffd744c5c..82eb36fc1ec4b 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.rs +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.rs @@ -1,11 +1,10 @@ impl Solution { pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { - let (mut f, mut g) = (0, 0); - for i in 2..=cost.len() { - let gg = std::cmp::min(f + cost[i - 2], g + cost[i - 1]); - f = g; - g = gg; + let n = cost.len(); + let mut f = vec![0; n + 1]; + for i in 2..=n { + f[i] = std::cmp::min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]); } - g + f[n] } } From d9c7fd15d0bc3a0640a6b531ea253470154df8fb Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:46:12 +0800 Subject: [PATCH 20/28] Update Solution2.ts --- .../0746.Min Cost Climbing Stairs/Solution2.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.ts b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.ts index 2b133cba36647..7c51ae65fec4f 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.ts +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution2.ts @@ -1,8 +1,8 @@ function minCostClimbingStairs(cost: number[]): number { - let a = 0, - b = 0; - for (let i = 1; i < cost.length; ++i) { - [a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])]; + const n = cost.length; + const f: number[] = Array(n + 1).fill(0); + for (let i = 2; i <= n; ++i) { + f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); } - return b; + return f[n]; } From adbafb6e26bf2e175984abd1a42cce13a2f28cce Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:46:28 +0800 Subject: [PATCH 21/28] Create Solution3.cpp --- .../0746.Min Cost Climbing Stairs/Solution3.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.cpp diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.cpp b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.cpp new file mode 100644 index 0000000000000..c03bc20db85fa --- /dev/null +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int minCostClimbingStairs(vector& cost) { + int f = 0, g = 0; + for (int i = 2; i <= cost.size(); ++i) { + int gg = min(f + cost[i - 2], g + cost[i - 1]); + f = g; + g = gg; + } + return g; + } +}; From 5242505362fc98690e3e9b962a15a7ca91d77da8 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:48:20 +0800 Subject: [PATCH 22/28] Create Solution3.go --- .../0700-0799/0746.Min Cost Climbing Stairs/Solution3.go | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.go diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.go b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.go new file mode 100644 index 0000000000000..8191ce111cd27 --- /dev/null +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.go @@ -0,0 +1,7 @@ +func minCostClimbingStairs(cost []int) int { + var f, g int + for i := 2; i <= n; i++ { + f, g = g, min(f+cost[i-2], g+cost[i-1]) + } + return g +} From fd827906ecf6a953f52d0a48eb5db9f114908bab Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:48:32 +0800 Subject: [PATCH 23/28] Create Solution3.java --- .../0746.Min Cost Climbing Stairs/Solution3.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.java diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.java b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.java new file mode 100644 index 0000000000000..a9bfc282cc000 --- /dev/null +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.java @@ -0,0 +1,11 @@ +class Solution { + public int minCostClimbingStairs(int[] cost) { + int f = 0, g = 0; + for (int i = 2; i <= cost.length; ++i) { + int gg = Math.min(f + cost[i - 2], g + cost[i - 1]); + f = g; + g = gg; + } + return g; + } +} From 64322de082c5febcd509aba53001f9a7276fc8b9 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:48:44 +0800 Subject: [PATCH 24/28] Update Solution3.js --- .../0746.Min Cost Climbing Stairs/Solution3.js | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.js b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.js index 5b0a7cde9f2c1..d35f4aa4a2238 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.js +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.js @@ -1,15 +1,7 @@ function minCostClimbingStairs(cost) { - const n = cost.length; - const cache = Array(n).fill(-1); - - const fn = i => { - if (i <= 1) return cost[i]; - if (cache[i] !== undefined && cache[i] !== -1) return cache[i]; - - cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2)); - - return cache[i]; - }; - - return fn(n); + let [f, g] = [0, 0]; + for (let i = 1; i < cost.length; ++i) { + [f, g] = [g, Math.min(f + cost[i - 1], g + cost[i])]; + } + return g; } From a25333d9fd67a1de24575154bdd99f758431884c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:48:57 +0800 Subject: [PATCH 25/28] Create Solution3.py --- .../0700-0799/0746.Min Cost Climbing Stairs/Solution3.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.py diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.py b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.py new file mode 100644 index 0000000000000..bc756e0b55d92 --- /dev/null +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.py @@ -0,0 +1,6 @@ +class Solution: + def minCostClimbingStairs(self, cost: List[int]) -> int: + f = g = 0 + for i in range(2, len(cost) + 1): + f, g = g, min(f + cost[i - 2], g + cost[i - 1]) + return g From 4f59de66de4e7925b1a783cdb969f18fe5cb9a06 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:49:08 +0800 Subject: [PATCH 26/28] Create Solution3.rs --- .../0746.Min Cost Climbing Stairs/Solution3.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.rs diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.rs b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.rs new file mode 100644 index 0000000000000..5d62ffd744c5c --- /dev/null +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.rs @@ -0,0 +1,11 @@ +impl Solution { + pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { + let (mut f, mut g) = (0, 0); + for i in 2..=cost.len() { + let gg = std::cmp::min(f + cost[i - 2], g + cost[i - 1]); + f = g; + g = gg; + } + g + } +} From ccdefcb0abd55c6d6b78a36f96b8bb437153ff73 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:49:20 +0800 Subject: [PATCH 27/28] Update Solution3.ts --- .../0746.Min Cost Climbing Stairs/Solution3.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.ts b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.ts index bced8b2a41393..978f5b56d2ff7 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.ts +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/Solution3.ts @@ -1,15 +1,7 @@ function minCostClimbingStairs(cost: number[]): number { - const n = cost.length; - const cache = Array(n).fill(-1); - - const fn = (i: number): number => { - if (i <= 1) return cost[i]; - if (cache[i] !== undefined && cache[i] !== -1) return cache[i]; - - cache[i] = (cost[i] ?? 0) + Math.min(fn(i - 1), fn(i - 2)); - - return cache[i]; - }; - - return fn(n); + let [f, g] = [0, 0]; + for (let i = 1; i < cost.length; ++i) { + [f, g] = [g, Math.min(f + cost[i - 1], g + cost[i])]; + } + return g; } From ecc41be8c1071d0a3d1f3a59ed1558026fc7007c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 11 Jul 2024 09:52:15 +0800 Subject: [PATCH 28/28] Update README.md --- solution/0700-0799/0746.Min Cost Climbing Stairs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md index d2162dec5227c..9f5ff416cdbfc 100644 --- a/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md +++ b/solution/0700-0799/0746.Min Cost Climbing Stairs/README.md @@ -67,7 +67,7 @@ tags: ### 方法一:记忆化搜索 -我们设计一个函数 $textit{dfs}(i)$,表示从第 $i$ 个阶梯开始爬楼梯所需要的最小花费。那么答案为 $\min(\textit{dfs}(0), \textit{dfs}(1))$。 +我们设计一个函数 $\textit{dfs}(i)$,表示从第 $i$ 个阶梯开始爬楼梯所需要的最小花费。那么答案为 $\min(\textit{dfs}(0), \textit{dfs}(1))$。 函数 $\textit{dfs}(i)$ 的执行过程如下: