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

Added climb_stairs_with_min_moves #273

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions CPP/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ Format: -[Program name](name of the file)
[N-Queens Proplem](N-QueensProblem.cpp)

[Topological Sorting](Topological_sorting.cpp)

[Climbing stairs with min moves](climb_stairs_with_min_moves.cpp)
49 changes: 49 additions & 0 deletions CPP/climb_stairs_with_min_moves.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include<bits/stdc++.h>
using namespace std;

int main()
{

int n;
cout<<"Enter the number of stairs:"<<endl;
cin>>n; // number of stairs
int jumps[n+1]={0};

cout<<"Enter the jumps at each stairs"<<endl;
for(int i=0;i<n;i++)
cin>>jumps[i];
queue<int>q;
for(int i=0;i<n;i++)
q.push(i);

int dp[n];
int x=q.front();
dp[0]=0;
for(int i=1;i<n;)
{

if(i<=jumps[x]+x) //checking the condition
dp[i]=dp[x]+1,i++;
else
{
x=q.front();
q.pop();

}
}
cout<<dp[n-1]; //printing min jumps required to reach last stairs.
}

/*
Input : number of stairs, jumps at every stairs
Output: minimum jumps to reach last stairs.


Sample input :
5
[2,3,1,1,4]

Output : 2


*/
Binary file added CPP/climb_strairs_with_min_moves.exe
Binary file not shown.