Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

Climb stairs(dp) #253

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Java/ClimbStairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.util.Scanner;

public class climbstaris {

public static void main(String[] args) {

// Taking input from user

Scanner scn = new Scanner(System.in);
int n = scn.nextInt();

int[] arr = new int[n];
for(int i = 0;i<n;i++){
arr[i] = scn.nextInt();
}

// creating an array for DP

Integer[] dp = new Integer[n+1];
dp[n] = 0;

for(int i=n-1;i>=0;i--){
if(arr[i]>0){
int m = Integer.MAX_VALUE;
for(int j = 1;j<=arr[i] && i+j<n+1;j++){
if(dp[i+j]!=null)
m= Math.min(m,dp[i+j]); //calc. best possible way

}
if(m != Integer.MAX_VALUE)
dp[i]=m+1;

}
}

// Displaying result

System.out.println(dp[0]);
}
}

/*
------------------------------------------------------------------
JAVA program for climb stairs with minimum moves.
Input: Size of stairs and the maximum value to which user can jump
Output: minimum number of moves, in which we can reach to the top of staircase.
-------------------------------------------------------------------
Sample input 1:
10
3 3 0 2 1 2 4 2 0 0

Output:
4
-------------------------------------------------------------------
Time Complexity : O(n^2)
Space Complexity : O(n)
-------------------------------------------------------------------
*/
2 changes: 2 additions & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Add links to your code in Alphabetical Order.

[Bubble Sort](./bubbleSort.java)

[Climb Stairs with minimum moves using DP](./ClimbStairs.java)

[Fibonacci Series](./Fibonacci Program.java)

[Gold mine Problem](./Goldmine.java)
Expand Down