Skip to content

Commit 84d0e29

Browse files
add 746
1 parent 44908b6 commit 84d0e29

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Your ideas/fixes/algorithms are more than welcome!
2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
2525
|748|[Shortest Completing Word](https://leetcode.com/problems/shortest-completing-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_748.java) | O(n) | O(1) | Easy|
26+
|746|[Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_746.java) | O(n) | O(1) | Easy|
2627
|744|[Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_744.java) | O(logn) | O(1) | Easy|
2728
|739|[Daily Temperatures](https://leetcode.com/problems/daily-temperatures/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_739.java) | O(n^2) | O(1) | Medium|
2829
|738|[Monotone Increasing Digits](https://leetcode.com/problems/monotone-increasing-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_738.java) | O(n) | O(1) | Medium|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder.solutions;
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+
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] = Math.min(cost[1], cost[0] + 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]);
35+
}
36+
}
37+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._746;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _746Test {
10+
private static _746.Solution1 solution1;
11+
private static int[] cost;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _746.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
cost = new int[] {10, 15, 20};
21+
assertEquals(15, solution1.minCostClimbingStairs(cost));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
cost = new int[] {1, 100, 1, 1, 1, 100, 1, 1, 100, 1};
27+
assertEquals(6, solution1.minCostClimbingStairs(cost));
28+
}
29+
}

0 commit comments

Comments
 (0)