File tree 5 files changed +69
-0
lines changed
algorithms/MinCostClimbingStairs
5 files changed +69
-0
lines changed Original file line number Diff line number Diff line change @@ -145,6 +145,7 @@ All solutions will be accepted!
145
145
| 724| [ Find Pivot Index] ( https://leetcode-cn.com/problems/find-pivot-index/description/ ) | [ java/py/js] ( ./algorithms/FindPivotIndex ) | Easy|
146
146
| 53| [ Maximum Subarray] ( https://leetcode-cn.com/problems/maximum-subarray/description/ ) | [ java/py/js] ( ./algorithms/MaximumSubarray ) | Easy|
147
147
| 543| [ Diameter Of Binary] ( https://leetcode-cn.com/problems/diameter-of-binary-tree/description/ ) | [ java/py/js] ( ./algorithms/DiameterOfBinary ) | Easy|
148
+ | 746| [ Min Cost Climbing Stairs] ( https://leetcode-cn.com/problems/min-cost-climbing-stairs/description/ ) | [ java/py/js] ( ./algorithms/MinCostClimbingStairs ) | Easy|
148
149
149
150
# Database
150
151
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Min Cost Climbing Stairs
2
+ This problem is easy to solve by dynamic programming, the recursion solution is like below:
3
+ ```
4
+ class Solution(object):
5
+ def minCostClimbingStairs(self, cost):
6
+ """
7
+ :type cost: List[int]
8
+ :rtype: int
9
+ """
10
+ if len(cost) < 2:
11
+ return 0
12
+ elif len(cost) == 2:
13
+ return min(cost[0], cost[1])
14
+ else:
15
+ return min(self.minCostClimbingStairs(cost[:-2]) + cost[-2], self.minCostClimbingStairs(cost[:-1]) + cost[-1])
16
+ ```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int minCostClimbingStairs (int [] cost ) {
3
+ List <Integer > minCosts = new ArrayList <Integer >();
4
+
5
+ for (int c : cost ) {
6
+ int size = minCosts .size ();
7
+
8
+ if (size < 2 ) {
9
+ minCosts .add (c );
10
+ } else {
11
+ minCosts .add (c + Math .min (minCosts .get (size - 2 ), minCosts .get (size - 1 )));
12
+ }
13
+ }
14
+ int size = minCosts .size ();
15
+ return size < 2 ? 0 : Math .min (minCosts .get (size - 2 ), minCosts .get (size - 1 ));
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } cost
3
+ * @return {number }
4
+ */
5
+ var minCostClimbingStairs = function ( cost ) {
6
+ let minCosts = [ ]
7
+
8
+ cost . forEach ( c => {
9
+ let length = minCosts . length
10
+ if ( length < 2 ) {
11
+ minCosts . push ( c )
12
+ } else {
13
+ minCosts . push ( c + Math . min ( minCosts [ length - 2 ] , minCosts [ length - 1 ] ) )
14
+ }
15
+ } )
16
+
17
+ let length = minCosts . length
18
+ return length < 2 ? 0 : Math . min ( minCosts [ length - 2 ] , minCosts [ length - 1 ] )
19
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def minCostClimbingStairs (self , cost ):
3
+ """
4
+ :type cost: List[int]
5
+ :rtype: int
6
+ """
7
+ # the min cost of every stair
8
+ min_costs = []
9
+
10
+ for c in cost :
11
+ if len (min_costs ) < 2 :
12
+ min_costs .append (c )
13
+ else :
14
+ min_costs .append (c + min (min_costs [- 2 ], min_costs [- 1 ]))
15
+
16
+ return 0 if len (min_costs ) < 2 else min (min_costs [- 2 ], min_costs [- 1 ])
You can’t perform that action at this time.
0 commit comments