@@ -130,32 +130,134 @@ source: Biweekly Contest 166 Q2
130130
131131<!-- solution:start -->
132132
133- ### Solution 1
133+ ### Solution 1: Dynamic Programming
134+
135+ We define $f[ i] $ as the minimum total cost required to reach the $i$-th stair, initially $f[ 0] = 0$, and all other $f[ i] = +\infty$.
136+
137+ For each stair $i$, we can jump from the $(i-1)$-th, $(i-2)$-th, or $(i-3)$-th stair, so we have the following state transition equation:
138+
139+ $$
140+ f[i] = \min_{j=i-3}^{i-1} (f[j] + \textit{costs}[i - 1] + (i - j)^2)
141+ $$
142+
143+ Where $\textit{costs}[ i] $ is the cost of the $i$-th stair, and $(i - j)^2$ is the jump cost from the $j$-th stair to the $i$-th stair. Note that we need to ensure $j$ is not less than $0$.
144+
145+ The final answer is $f[ n] $.
146+
147+ The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of stairs.
134148
135149<!-- tabs:start -->
136150
137151#### Python3
138152
139153``` python
140-
154+ class Solution :
155+ def climbStairs (self , n : int , costs : List[int ]) -> int :
156+ n = len (costs)
157+ f = [inf] * (n + 1 )
158+ f[0 ] = 0
159+ for i, x in enumerate (costs, 1 ):
160+ for j in range (i - 3 , i):
161+ if j >= 0 :
162+ f[i] = min (f[i], f[j] + x + (i - j) ** 2 )
163+ return f[n]
141164```
142165
143166#### Java
144167
145168``` java
146-
169+ class Solution {
170+ public int climbStairs (int n , int [] costs ) {
171+ int [] f = new int [n + 1 ];
172+ final int inf = Integer . MAX_VALUE / 2 ;
173+ Arrays . fill(f, inf);
174+ f[0 ] = 0 ;
175+ for (int i = 1 ; i <= n; ++ i) {
176+ int x = costs[i - 1 ];
177+ for (int j = Math . max(0 , i - 3 ); j < i; ++ j) {
178+ f[i] = Math . min(f[i], f[j] + x + (i - j) * (i - j));
179+ }
180+ }
181+ return f[n];
182+ }
183+ }
147184```
148185
149186#### C++
150187
151188``` cpp
152-
189+ class Solution {
190+ public:
191+ int climbStairs(int n, vector<int >& costs) {
192+ vector<int > f(n + 1, INT_MAX / 2);
193+ f[ 0] = 0;
194+ for (int i = 1; i <= n; ++i) {
195+ int x = costs[ i - 1] ;
196+ for (int j = max(0, i - 3); j < i; ++j) {
197+ f[ i] = min(f[ i] , f[ j] + x + (i - j) * (i - j));
198+ }
199+ }
200+ return f[ n] ;
201+ }
202+ };
153203```
154204
155205#### Go
156206
157207```go
208+ func climbStairs(n int, costs []int) int {
209+ const inf = int(1e9)
210+ f := make([]int, n+1)
211+ for i := range f {
212+ f[i] = inf
213+ }
214+ f[0] = 0
215+ for i := 1; i <= n; i++ {
216+ x := costs[i-1]
217+ for j := max(0, i-3); j < i; j++ {
218+ f[i] = min(f[i], f[j]+x+(i-j)*(i-j))
219+ }
220+ }
221+ return f[n]
222+ }
223+ ```
224+
225+ #### TypeScript
226+
227+ ``` ts
228+ function climbStairs(n : number , costs : number []): number {
229+ const inf = Number .MAX_SAFE_INTEGER / 2 ;
230+ const f = Array (n + 1 ).fill (inf );
231+ f [0 ] = 0 ;
232+
233+ for (let i = 1 ; i <= n ; ++ i ) {
234+ const x = costs [i - 1 ];
235+ for (let j = Math .max (0 , i - 3 ); j < i ; ++ j ) {
236+ f [i ] = Math .min (f [i ], f [j ] + x + (i - j ) * (i - j ));
237+ }
238+ }
239+ return f [n ];
240+ }
241+ ```
158242
243+ #### Rust
244+
245+ ``` rust
246+ impl Solution {
247+ pub fn climb_stairs (n : i32 , costs : Vec <i32 >) -> i32 {
248+ let n = n as usize ;
249+ let inf = i32 :: MAX / 2 ;
250+ let mut f = vec! [inf ; n + 1 ];
251+ f [0 ] = 0 ;
252+ for i in 1 ..= n {
253+ let x = costs [i - 1 ];
254+ for j in (i . saturating_sub (3 )).. i {
255+ f [i ] = f [i ]. min (f [j ] + x + ((i - j ) * (i - j )) as i32 );
256+ }
257+ }
258+ f [n ]
259+ }
260+ }
159261```
160262
161263<!-- tabs: end -->
0 commit comments