Skip to content

Commit

Permalink
Create 0746_Min_Cost_Climbing_Stairs.java
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhuzaima committed Oct 14, 2023
1 parent 9a5c207 commit 14832c8
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 0746_Min_Cost_Climbing_Stairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// id: 746
// Name: Min Cost Climbing Stairs
// link: https://leetcode.com/problems/min-cost-climbing-stairs/?envType=daily-question
// Difficulty: Easy

class Solution {
public int minCostClimbingStairs(int[] cost) {
int step_minus_2 = cost[0]; // stores cost till step_minus_2
int step_minus_1 = cost[1]; // stores cost till step_minus_1

for (int i = 2; i < cost.length; i++) {
// reach ith step from step with minimum cost
int c = cost[i] + Math.min(step_minus_2, step_minus_1);

step_minus_2 = step_minus_1;
step_minus_1 = c;

}
return Math.min(step_minus_2, step_minus_1);
}
}

0 comments on commit 14832c8

Please sign in to comment.