-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 0746_Min_Cost_Climbing_Stairs.java
- Loading branch information
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |