28.) 1D- DP

1D dp questions-
1. fibonacii✅
2. climbing stairs (fibo 2.0)✅
3. frog jump✅
4. House Robber-1✅
5. House Robber-2✅

------------------------------------------------------------------------------------------------------------
How to identify a DP ques-
it involves concept of exploring all possible ways and then find the total count or the best possible way which gives min or max
-------------------------------------------------------------------------------------------------------------

Shortcut to solve a DP ques-
                                    4. find what does f(n) signifies
                                    5. write recursive soln..
                                    6. find overlapping subproblems within recursion tree.
                                    7. memoization ki feel ke liye har case pe recursion se calculation krke dekhlo
                                    8. look at parameters which are changing
                                  9. declare dp of n+1 size--> before returning update the dp--->whenver u call a                                            recursion before doing so if it exists already in the dp
                                    10. tabulation k liye recursion tree me se bottom to up ka krenge
me-
1. recursion tree banalo
2. recursive soln banao 
3. ab convert kro memoization mein, agar 0 se n mein ni hora toh n se 0 wale logic se dekho
----------------------------------------------------------------------------------------------------------------------------
1. Climbing stairs-
we will mark no. of stairs as indices
the function will return total no. of ways

Memoization-

Tabulation-












------------------------------------------------------------------------------------------------------------------------

2.  Frog-Jump:

here, f(n-1) means min energy reqd to reach n-1 from 0th index

recursive soln-















-----------------------------------------------------------------------------------
memoization-


-------------------------------------------------------------------------------
tabulation- 

----------------------------------------------
space-optimisation:-


--------------------------------------------------------------------------------------------------------------------------

THUMB-RULE: 
whenever there is ind-1 and ind-2 in a soln of dp, there will be always space optmisation possible there...
-----------------------------------------------------------------------------------------------------------------------------
 FROG JUMPS-2

Now you can jump not only i+1 or i+2 , but upto i+k








time- O(k*n),     space- O(2n)

time- O(nk),   space-O(n)







can't space optimise to O(1)

-----------------------------------------------------------------------------------------------------------------------------

MAX SUM OF NON- ADJACENTS/ HOUSE ROBBER-1

sort of dp on subseq with pick or not pick technique

for memoization we define f(n) as the max robbery we can do till index n

there are n states, so time complexity reduced to O(n)

tabulation-


---------------------------------------------
space optimisation-
time-o(n) & space-o(1)
----------------------------------------------------------------------------------------------------------------------------

 HOUSE ROBBER-2
Now houses are arranged on circle...
same as prvs ques just one extra thing now that first and last elmts are also now the adjacent elements

RECURSIVE-
MEMOIZATION-
TABULATION-
SPACE OPTIMISATION-

--------------------------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

30.) DP on subsequences

19. Strings (LB)

32. Binary Trees-2 (bfs/dfs med problems)