27. Dynamic Programming

 









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

NOTE-

memoization tends to store value of sub problems in some map/table

Recursion mean top to down means reqd ans se base case tak jaao

Tabulation means bottom to up means base case se reqd ans tak jaao

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

By recursion-



Steps to convert a recursion to a DP solution-

1. declare a dp array, considering the size of the sub problems (e.g., fibonacci mein recursion tree mein f(n) se f(0) tak jaate hue total n+1 nodes milenge at max )

2. store answer for every sub problem which is being computed

3. checking if subproblem is previously solved

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

MEMOIZATION-

Now time complexity reduced from exponential to O(n) since worst mein n lagega baakiyo mein toh const time mein ho jayega...

SPACE- O(n)
---------------------------------------------------------------------------------------------------------------------------

Tabulation method-

go from base case to reqd ans

1. base cases se initial values likhlo dp array ki
2. recursive relation mein fn ki jagah dp likhlo and loop m lagado starting idx dekh k

time- O(n)       space- O(n)

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

optimizing space- (instead of array we will use variables)- O(1) space


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

Comments

Popular posts from this blog

30.) DP on subsequences

19. Strings (LB)

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