|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * 746. Min Cost Climbing Stairs |
5 |
| - * |
6 |
| - * On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). |
7 |
| - * Once you pay the cost, you can either climb one or two steps. |
8 |
| - * You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1. |
9 |
| -
|
10 |
| - Example 1: |
11 |
| - Input: cost = [10, 15, 20] |
12 |
| - Output: 15 |
13 |
| - Explanation: Cheapest is start on cost[1], pay that cost and go to the top. |
14 |
| -
|
15 |
| - Example 2: |
16 |
| - Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] |
17 |
| - Output: 6 |
18 |
| - Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3]. |
19 |
| -
|
20 |
| - Note: |
21 |
| - cost will have a length in the range [2, 1000]. |
22 |
| - Every cost[i] will be an integer in the range [0, 999]. |
23 |
| - */ |
24 |
| - |
25 | 3 | public class _746 {
|
26 |
| - public static class Solution1 { |
27 |
| - public int minCostClimbingStairs(int[] cost) { |
28 |
| - int[] dp = new int[cost.length]; |
29 |
| - dp[0] = cost[0]; |
30 |
| - dp[1] = cost[1]; |
31 |
| - for (int i = 2; i < cost.length; i++) { |
32 |
| - dp[i] = Math.min(dp[i - 1] + cost[i], dp[i - 2] + cost[i]); |
33 |
| - } |
34 |
| - return Math.min(dp[cost.length - 1], dp[cost.length - 2]); |
| 4 | + public static class Solution1 { |
| 5 | + public int minCostClimbingStairs(int[] cost) { |
| 6 | + int[] dp = new int[cost.length]; |
| 7 | + dp[0] = cost[0]; |
| 8 | + dp[1] = cost[1]; |
| 9 | + for (int i = 2; i < cost.length; i++) { |
| 10 | + dp[i] = Math.min(dp[i - 1] + cost[i], dp[i - 2] + cost[i]); |
| 11 | + } |
| 12 | + return Math.min(dp[cost.length - 1], dp[cost.length - 2]); |
| 13 | + } |
35 | 14 | }
|
36 |
| - } |
37 | 15 | }
|
0 commit comments