File tree 1 file changed +10
-4
lines changed
min_cost_climbing_stairs/src
1 file changed +10
-4
lines changed Original file line number Diff line number Diff line change 1
1
pub struct Solution { }
2
2
3
3
impl Solution {
4
- fn min_cost_to_step ( cost : & Vec < i32 > , i : usize ) -> i32 {
4
+ fn min_cost_to_step ( cost : & Vec < i32 > , cache : & mut Vec < Option < i32 > > , i : usize ) -> i32 {
5
5
if i < 2 {
6
6
return 0 ;
7
7
}
8
- ( Self :: min_cost_to_step ( cost, i - 1 ) + cost[ i - 1 ] )
9
- . min ( Self :: min_cost_to_step ( cost, i - 2 ) + cost[ i - 2 ] )
8
+ if let Some ( res) = cache[ i] {
9
+ return res;
10
+ }
11
+ let res = ( Self :: min_cost_to_step ( cost, cache, i - 1 ) + cost[ i - 1 ] )
12
+ . min ( Self :: min_cost_to_step ( cost, cache, i - 2 ) + cost[ i - 2 ] ) ;
13
+ cache[ i] = Some ( res) ;
14
+ res
10
15
}
11
16
12
17
pub fn min_cost_climbing_stairs ( cost : Vec < i32 > ) -> i32 {
13
- Self :: min_cost_to_step ( & cost, cost. len ( ) )
18
+ let mut cache = vec ! [ None ; cost. len( ) + 1 ] ;
19
+ Self :: min_cost_to_step ( & cost, & mut cache, cost. len ( ) )
14
20
}
15
21
}
16
22
You can’t perform that action at this time.
0 commit comments