Climbing Stairs with Jumps using Dynamic Programming | Recursive Staircase Problem

  Рет қаралды 77,794

Pepcoding

Pepcoding

Күн бұрын

Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we discuss the Climbing Stairs problem with variable jumps using dynamic programming and tabulation method. This problem is also referred to as Recursive Staircase Problem and an asked problem in Amazon. In this problem,
1. You are given a number n, representing the number of stairs in a staircase.
2. You are on the 0th step and are required to climb to the top.
3. You are given n numbers, where ith element's value represents - till how far from the step you could jump to in a single move.
4. You are required to print the number of different paths via which you can climb to the top.
For a better experience and more exercises, VISIT: www.pepcoding....
#dp #dynamicprogramming #climbstairs
Have a look at our result: www.pepcoding....
Follow us on our FB page: / pepcoding
Follow us on Instagram: / pepcoding
Follow us on LinkedIn: / pepcoding-education

Пікірлер: 136
@manishkumarsharma6507
@manishkumarsharma6507 3 жыл бұрын
I regret why i knew this channel now.... I should have aware of this channel 1 year back..... Best coding channel on youtube ..... With free content
@karmakaraayon4238
@karmakaraayon4238 Жыл бұрын
same yar 🥹
@faizanhaider3953
@faizanhaider3953 Жыл бұрын
Same yr
@mdarman0063
@mdarman0063 4 ай бұрын
Same case with me😢
@arfataara4329
@arfataara4329 4 жыл бұрын
Highly under appreciated channel. Learning a lot.
@Pepcoding
@Pepcoding 4 жыл бұрын
thank you beta. agar content acha lag rha ho to review daal do g.page/Pepcoding/review?rc
@adityadev2825
@adityadev2825 3 жыл бұрын
Marvelous playlist 🔥 For those who are confused on how to travel in reverse direction i.e, from 0 -> 6. Problems can be solved in any direction based on the meaning you assign to each cell. Code for reverse order: int[] dp = new int[n+1]; dp[0] = 1; for(int i=0; i
@tanishqyadav6053
@tanishqyadav6053 2 жыл бұрын
The Memoized as well as Tabulated code in CPP for this question : #include using namespace std; long long int Sol_Memo_Util( long long int k, long long int n , vector &arr,vector &memo) { if(k>n) return 0; if(k==n) return 1; if(memo[k]!=0) return memo[k]; long long int res = 0; for(long long int i=1;i=0;i--) { for(long long int j=1;j
@PiyushKumar-po8bj
@PiyushKumar-po8bj Жыл бұрын
can this be done using recusion ?
@trainwithguneet
@trainwithguneet 3 жыл бұрын
You are super underrated. I did a variation of this question earlier on GFG (min jumps to reach end of array).
@sairagavender
@sairagavender Күн бұрын
i am just watching this cause i am bored at this point even though i am not a cs student
@seemaalam3127
@seemaalam3127 2 жыл бұрын
First dialogue...lol...😂😂 Lots of love for the way u teach...😍
@Pepcoding
@Pepcoding 2 жыл бұрын
Thank you so much 😀Please visit nados.pepcoding.com for more such amazing content :)
@YashGupta-zh8fz
@YashGupta-zh8fz 4 жыл бұрын
Wow sir Glad to finally find this channel and your videos Your teaching is excellent
@Pepcoding
@Pepcoding 4 жыл бұрын
Padhte rahie. Learn well.
@sachinvishwakarma6607
@sachinvishwakarma6607 3 жыл бұрын
Please put this intro in every explanation 😭😂😂😂
@nightingale180
@nightingale180 Жыл бұрын
Hey, the content is good but the memes in b/w become too loud as compared to your voice so have to adjust the sound for that..just afeedback. Else thank you so much for your effort, really appreciate that..
@mayankverma3690
@mayankverma3690 3 жыл бұрын
Sir in this question the direction is from right to left in tabulation , can we solve it using tabulation using the left to right direction??
@Pepcoding
@Pepcoding 3 жыл бұрын
Hanji beta, dono tarf se kr skte h. Dp sochne ka tarika alg h aur dp ka meaning change ho jayega bus.
@tejaswinisingh6263
@tejaswinisingh6263 3 жыл бұрын
Sir , which method is better tabulation or memoization, if I want to do in one way only:)
@amantiwari1481
@amantiwari1481 3 жыл бұрын
Memoization because it is difficult and sometime illogical to solve in tabulation method. But if you are good with recursion then you can solve dp easily with proper logic.
@prateekchaurasiya3877
@prateekchaurasiya3877 4 жыл бұрын
Sir really wow.... and memes in between are lit
@Pepcoding
@Pepcoding 4 жыл бұрын
Ji. Keep giving us a feedback. Usi tarah ka content bnaenge jo aapko jyada pasand aaega.
@tushar_kushwaha
@tushar_kushwaha 3 жыл бұрын
sir app apni marketing pe bhi dhyaan dijiye...aur aap bhi jyda se jyda apne channel pe aaya kare aur bhi short videos banaye. See coding ninjas channel for reference. Baaki aap jo kr rhe hai bdiya hai. Thank you.
@dark_techyy
@dark_techyy 3 жыл бұрын
Sir mai apka regular student hu...🙏..pr aapne kaha tha agr recursion use ni kiya to dp nhi h vo...or ye tabulation wale method me jb loop use kr rhe h to vo dp hai k ni?🤔
@pravinmudaliyar3421
@pravinmudaliyar3421 2 жыл бұрын
Memoized as well as tabulated approach: public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for(int i=0; i arr.length){ return 0; } if(dp[curr] != 0){ return dp[curr]; } int ways = 0; for(int i=1; i=0; i--){ int jumps = arr[i]; for(int j=1; j
@pragatirawat1103
@pragatirawat1103 3 жыл бұрын
Some of you are asking for memoization code for the same problem. Commenting below the memoized code. Please have a look :- import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner scn = new Scanner(System.in) ; int num = scn.nextInt() ; int[] qb = new int[num+1]; int n = 0; int[] arr = new int[num] ; for(int i = 0; i < arr.length; i++){ arr[i] = scn.nextInt() ; } int cp = countPaths( num,n,qb,arr ); System.out.println(cp); } public static int countPaths( int num,int n,int[] qb,int[] arr ){ if(n == num) { return 1; } else if(n > num) { return 0; } if(qb[n] > 0) { return qb[n]; } int cp = 0; for(int i = 1; i
@satisfyingwalks4010
@satisfyingwalks4010 2 жыл бұрын
thank you for this!
@curiosity9861
@curiosity9861 3 жыл бұрын
I have checked it multiple times but I am getting 15 as an output for the test case provided in the Pepcoding website where the output for that test case is 5 .Can you please explain that. testcase
@sadmansakib007
@sadmansakib007 3 жыл бұрын
you need to step on "just outside" the array. hope that helps
@aaswinsinha3698
@aaswinsinha3698 3 жыл бұрын
Sir please ek video banao ki how to identify a problem can be solved with dp or not?
@AnkitSharma-wj2tb
@AnkitSharma-wj2tb 4 жыл бұрын
Superb sir. Nice way of teaching 😂😂😂🔥🔥❤️❤️❤️
@Pepcoding
@Pepcoding 4 жыл бұрын
Thanks a lot 😊
@vaibhavaggarwal5710
@vaibhavaggarwal5710 11 күн бұрын
It canbe done as bottoms up as well
@Hephaestus-q9h
@Hephaestus-q9h Жыл бұрын
the compiler on site does not work?
@swapnilpadaya163
@swapnilpadaya163 4 жыл бұрын
sir iska recursive solution ka video banna do na please.
@ztrixx3280
@ztrixx3280 3 жыл бұрын
I tried a lot, but couldn't do it using recursion. Need help!!
@maxbrooke9411
@maxbrooke9411 3 жыл бұрын
same
@utkarsh_108
@utkarsh_108 3 жыл бұрын
@@maxbrooke9411 koini mai karke bhejta hu
@utkarsh_108
@utkarsh_108 3 жыл бұрын
int climbStairsWithVariableJumps(vector arr, int i, vectorqb){ // write your code here if(i==arr.size()){ qb[i]=1; return qb[i]; } if(qb[i]!=-1){ return qb[i]; } int count=0; for(int step=1; stepn; vector arr(n); vector qb(n+1,-1); for(int i = 0 ; i < n ;i++){ cin>>arr[i]; } cout
@nivealokhande2153
@nivealokhande2153 Жыл бұрын
@@utkarsh_108 bhej jldi saal hogyaa
@himanshuchhikara4918
@himanshuchhikara4918 4 жыл бұрын
Hahaha 😂😂 mst editing hai sir mzza aagya
@Pepcoding
@Pepcoding 4 жыл бұрын
Hahaha 😂😂
@TheShantanu1395
@TheShantanu1395 2 жыл бұрын
Python solution for above problem def takeInput(): arr = [] for _ in range(int(input())): arr.append(int(input())) return arr def compute(arr): n = len(arr) dp = [0]*(n+1) dp[n] = 1 for i in range(n - 1, -1, -1): for j in range(1, arr[i] + 1): if i + j < len(dp): dp[i] += dp[i + j] return dp[0] print(compute(takeInput()))
@CD-xd6fm
@CD-xd6fm 2 жыл бұрын
what will happen in j loop if arr[i] =0 won't it give error ? (i am saying about when we can go 0 steps ahead)
@harshitgupta8844
@harshitgupta8844 2 жыл бұрын
how to write recursive code of this code. or how to solve this question via recursion please explain
@lifeskills5913
@lifeskills5913 2 жыл бұрын
Is there any difference between this question and question of climbing stairs with 1 or 2 jumps only?
@kartikpapney9250
@kartikpapney9250 4 жыл бұрын
Placement k baad aapse milne aaunga... Aapke charan sparsh krne hai 😍
@Pepcoding
@Pepcoding 4 жыл бұрын
jarur milne aao... placement k baad toh mithaai bhi leke aana! :)
@mkshubham1386
@mkshubham1386 3 жыл бұрын
Placement hui kya bhai?
@saichaithrik7134
@saichaithrik7134 Жыл бұрын
respected sir i have seen so many teachers during my learning process but u sir are the best of the bunch sir thanks for all the help.
@anuragnegi6423
@anuragnegi6423 4 жыл бұрын
ye question recursively kardiya 1st ques ki tarah, ye bhi dp mein included h??
@Pepcoding
@Pepcoding 4 жыл бұрын
hanji DP mei bhi hai
@horridhenry6789
@horridhenry6789 4 жыл бұрын
kaise bhai??
@NiteshKumar-nx2fu
@NiteshKumar-nx2fu 3 жыл бұрын
bhai recursion ka code comment krdo please...
@RAHUL-gf3ft
@RAHUL-gf3ft 3 жыл бұрын
bro pls send ur recursion code
@geetanegi2736
@geetanegi2736 3 жыл бұрын
Sir time complexity kab sikhayenga sir. Nice content sir .god bless u sir.
@Pepcoding
@Pepcoding 3 жыл бұрын
Beta, jaldi he daalege and thankyou! I am glad you liked it. If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )
@mahavirsingh5790
@mahavirsingh5790 3 жыл бұрын
Iterator I = "Meri shaktiyo ka galat istemal hua hai maa" 😀😀
@Pepcoding
@Pepcoding 3 жыл бұрын
Haha..😋
@sahilgupta9378
@sahilgupta9378 3 жыл бұрын
swaad aagya sir aapke tabularization se
@Pepcoding
@Pepcoding 3 жыл бұрын
Keep learning and keep loving Pepcoding😊 If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
@programmingexam9178
@programmingexam9178 Жыл бұрын
Why not in english?
@satyamgupta1416
@satyamgupta1416 3 жыл бұрын
sir is question mah direction q right to left li h jabki phle vale mah left to right li thi
@AbhishekSingh-qx1km
@AbhishekSingh-qx1km 3 жыл бұрын
What if arr[0]=0??
@yashparmar5722
@yashparmar5722 6 ай бұрын
Great❤
@omeshpal4249
@omeshpal4249 2 жыл бұрын
you are making a complex problem very easy and simple.. the way you are explaining is phenomenal.
@sakshamsrivastava6280
@sakshamsrivastava6280 3 жыл бұрын
IF YOU THINK IT THROUGH, IT MAKES QUITE A LOT OF SENSE. tHANKYOU sUMIT SIR
@gauravrewaliya3269
@gauravrewaliya3269 2 жыл бұрын
when to use tabulation and when memorization??
@dayamalik6025
@dayamalik6025 4 жыл бұрын
Super super super super teacher
@dayamalik6025
@dayamalik6025 4 жыл бұрын
Hello chachu your Aryan
@Pepcoding
@Pepcoding 4 жыл бұрын
Hello betu, Thank you. Yours, Chachu
@nivealokhande2153
@nivealokhande2153 Жыл бұрын
@@dayamalik6025 abee har video mai kyaa lga rkha hai hello
@anjneykumarsingh4461
@anjneykumarsingh4461 4 жыл бұрын
Sir,can every problem be solved by both tabulation and memoization
@Pepcoding
@Pepcoding 4 жыл бұрын
yes
@samiulkhan3744
@samiulkhan3744 2 жыл бұрын
no, to understand why it is not possible ,you can refer cheery pickup problem in the pepcoding level 2 dp series
@PiyushKumar-po8bj
@PiyushKumar-po8bj Жыл бұрын
can this be done using recusion ?
@NiteshKumar-nx2fu
@NiteshKumar-nx2fu 3 жыл бұрын
sir how can we do this using recursion??
@tusharmonga3253
@tusharmonga3253 3 жыл бұрын
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int n = Integer.parseInt(br.readLine()); int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) { arr[i] = Integer.parseInt(br.readLine()); } int[] dp = new int[n + 1]; Arrays.fill(dp,-1); System.out.println(climbStairsVarJumpDP(arr,0,dp)); } public static int climbStairsVarJumpDP(int[] arr, int i, int[] dp) { if (i == arr.length) { return dp[i] = 1; } if (dp[i] != -1) return dp[i]; int count = 0; for (int step = 1; step
@loserfruit9663
@loserfruit9663 3 жыл бұрын
@@tusharmonga3253 bro comment it in open section where everybody can see it,lots of student are facing problem in it using recursion
@tusharmonga3253
@tusharmonga3253 3 жыл бұрын
@@loserfruit9663 done
@loserfruit9663
@loserfruit9663 3 жыл бұрын
@@tusharmonga3253 great work
@zaid4you256
@zaid4you256 3 жыл бұрын
@@tusharmonga3253 bro thanks for this present
@tusharmonga3253
@tusharmonga3253 3 жыл бұрын
Recursion code : import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int n = Integer.parseInt(br.readLine()); int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) { arr[i] = Integer.parseInt(br.readLine()); } int[] dp = new int[n + 1]; Arrays.fill(dp,-1); System.out.println(climbStairsVarJumpDP(arr,0,dp)); } public static int climbStairsVarJumpDP(int[] arr, int i, int[] dp) { if (i == arr.length) { return dp[i] = 1; } if (dp[i] != -1) return dp[i]; int count = 0; for (int step = 1; step
@lakshmishree1281
@lakshmishree1281 2 жыл бұрын
thanks a lot sir
@manishkumarsah6898
@manishkumarsah6898 3 жыл бұрын
tusi great ho sir🙌
@tamannamirza2133
@tamannamirza2133 4 жыл бұрын
Sir, yahan ham 0 to 6 jaa rahein hain matlab shorter problem hua 6 to 6 and so on. Whereas last problem mein ham 0 to 0 short problem maan rahe the ye thoda confusion hai sir please clear kardijiye :( Dono problems mein ham stairs climb hi kar rahe hai lekin phir directions alag kyun
@Pepcoding
@Pepcoding 4 жыл бұрын
beta sikhane ke liye kia hai, ki dono tareeke se kar sakte hain. agar meaning alag dia to smaller problem doosri taraf banne lagegi aur travel ki driection badal jaegi. Sare questions dono tareeke se kie ja sakte hain. It all depends on meaning of cell
@tamannamirza2133
@tamannamirza2133 4 жыл бұрын
@@Pepcoding Okay sir thank you sooo much for taking time to solve my doubt. Abhi sab clear hogaya :) Feel really blessed to have found your channel
@aahanaganjewar9951
@aahanaganjewar9951 2 жыл бұрын
Recursive Solution class Solution { public boolean canJump(int[] nums) { return jump(nums, 0); } public boolean jump(int[] nums, int idx) { if (idx == nums.length - 1) { return true; } boolean ans = false; for (int i = 1; i
@priyankajain4223
@priyankajain4223 2 жыл бұрын
here we need to return count, not true and false, so how will it work
@aahanaganjewar9951
@aahanaganjewar9951 2 жыл бұрын
@@priyankajain4223 oh really sorry!
@priyankajain4223
@priyankajain4223 2 жыл бұрын
@@aahanaganjewar9951 Are you following this playlist?
@aahanaganjewar9951
@aahanaganjewar9951 2 жыл бұрын
@@priyankajain4223 yes!
@rohitnehra9519
@rohitnehra9519 2 жыл бұрын
Big fan maam
@pankajkaushik7912
@pankajkaushik7912 3 жыл бұрын
Sir dp(i)+=dp(i+j);.....Sir isme + symbol use kyun Kiya.....kyunki if we want to find dp(6),then dp(6)=dp(6)+dp(7)....Please help me sir
@Pepcoding
@Pepcoding 3 жыл бұрын
Hanji, sahe smjhe aap.
@pankajkaushik7912
@pankajkaushik7912 3 жыл бұрын
@@Pepcoding but sir dp(6) pehle hai hi nhi hmare pass
@KeshavKumar-zk3uh
@KeshavKumar-zk3uh 3 жыл бұрын
@@pankajkaushik7912 bhai dp(6) last element tha array ka, to wha jane ka ek rasta hamesha hota hai.
@pankajkaushik7912
@pankajkaushik7912 3 жыл бұрын
@@KeshavKumar-zk3uh Bhai kaise hota hai raasta elaborate?
@KeshavKumar-zk3uh
@KeshavKumar-zk3uh 3 жыл бұрын
@@pankajkaushik7912 generally ese me base cases me one leke chalte h ki 6 se 6 jane ka rasta ek hi taki uske realtive me ham soch paye fir 5 se 6 jane k kitne tarike honge,,,,confirm nhi h mujhe bhi itna hi samjh aaya
@divyanshsingh6163
@divyanshsingh6163 3 жыл бұрын
sir can you plz write dp[ i ] += dp[ i + j ] ; in some other way??
@Pepcoding
@Pepcoding 3 жыл бұрын
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
@crackthecode1372
@crackthecode1372 4 жыл бұрын
Awesome Video sir , Thanks a lot for uploading, sir just one suggestion: starting mein input output mention krdiya kijiye taaki pehle hum khud bji try krlein
@Pepcoding
@Pepcoding 4 жыл бұрын
ok ji
@karmakaraayon4238
@karmakaraayon4238 2 жыл бұрын
🔥
@Pepcoding
@Pepcoding 2 жыл бұрын
Hope you love the explanation, for better experience and well organised content visit - nados.io
@apoorvdixit2856
@apoorvdixit2856 4 жыл бұрын
sir content to bhot badhiya hai par , edting thodi zyada hogayi hai vedio mei. sorry sir but being one of your well wishers , isse brand value par thoda asar padhta hai, though content is best in the industry
@Pepcoding
@Pepcoding 4 жыл бұрын
Feedback is completely appreciated. Hanji, baad mei kar dia next videos mei.
@darkdemise8273
@darkdemise8273 4 жыл бұрын
Mujhe to maza aaya beech beech mei pata chalta rehta hai computer ke dusri side bhi insaan hai.
@shubhansusinghbhadoria8522
@shubhansusinghbhadoria8522 3 жыл бұрын
i loved it ,i think it gives the real time experience of attending your class.
@rishikeshrai6686
@rishikeshrai6686 4 жыл бұрын
Excellent sir
@Pepcoding
@Pepcoding 4 жыл бұрын
Keep watching
@oqant0424
@oqant0424 3 жыл бұрын
excellent explanation
@hemantsingh-zo3iw
@hemantsingh-zo3iw 4 жыл бұрын
Excellent Sir
@Pepcoding
@Pepcoding 4 жыл бұрын
Keep watching
@LegitGamer2345
@LegitGamer2345 3 жыл бұрын
is the time complexiy order of n?
@LegitGamer2345
@LegitGamer2345 3 жыл бұрын
is it order of n square ? for example steps are 3 and we can jump 3 steps at each level starting from ground
@anuragyadav1813
@anuragyadav1813 3 жыл бұрын
❤️❤️❤️
@darkdemise8273
@darkdemise8273 4 жыл бұрын
Sir aapke code se index out of bounds dikha raha thaa but pepcoding mei solutions mei 2 loop if(arr[i]>0)ki condition mei tha us se chal gayaa
@SCRIPTSAG
@SCRIPTSAG 3 жыл бұрын
Recursive solution kya hoga sir eska
@Pepcoding
@Pepcoding 3 жыл бұрын
Beta formula recursion ka same rahega. Bnane ki koshish karo
@SCRIPTSAG
@SCRIPTSAG 3 жыл бұрын
@@Pepcoding sir try ker rha hu mai soch pa rha hu recursion me kya kerunga but code me nhi ker pa rha hu subeh 4 bje se presan hu sir ab sirf tabultaion kerunga
@ajay8315
@ajay8315 3 жыл бұрын
@@Pepcoding Hello sir i do every problem with memoisation(recursion) but don't get an idea about how to solve print path type problem recusively. Like print lcs
@utkarsh_108
@utkarsh_108 3 жыл бұрын
@@SCRIPTSAG int climbStairsWithVariableJumps(vector arr, int i, vectorqb){ // write your code here if(i==arr.size()){ qb[i]=1; return qb[i]; } if(qb[i]!=-1){ return qb[i]; } int count=0; for(int step=1; stepn; vector arr(n); vector qb(n+1,-1); for(int i = 0 ; i < n ;i++){ cin>>arr[i]; } cout
🍉😋 #shorts
00:24
Денис Кукояка
Рет қаралды 3,6 МЛН
Will A Guitar Boat Hold My Weight?
00:20
MrBeast
Рет қаралды 261 МЛН
А ВЫ ЛЮБИТЕ ШКОЛУ?? #shorts
00:20
Паша Осадчий
Рет қаралды 9 МЛН
Every parent is like this ❤️💚💚💜💙
00:10
Like Asiya
Рет қаралды 18 МЛН
Mastering Midterm 231 Solutions | MATH 102 Recitation Breakdown
53:21
Byte-By-Byte | Python
Рет қаралды 5
Climbing Stairs - Dynamic Programming - Leetcode 70 - Python
18:08
5 Simple Steps for Solving Dynamic Programming Problems
21:27
Reducible
Рет қаралды 1 МЛН
Target Sum Subsets Dynamic Programming | Subset Sum Problem
29:20
LeetCode 55. Jump Game (Algorithm Explained)
10:06
Nick White
Рет қаралды 77 М.
DP 33. Edit Distance | Recursive to 1D Array Optimised Solution 🔥
37:39
🍉😋 #shorts
00:24
Денис Кукояка
Рет қаралды 3,6 МЛН