0% found this document useful (0 votes)
38 views23 pages

DP 1

PDF (No Image Break) The document discusses various dynamic programming problems and their solutions. It explains the three steps of solving a DP problem: 1) the recursion equation, 2) memoization by storing values in an array or vector, and 3) tabulation using a bottom-up approach. Several DP problems are then covered, including Fibonacci numbers, 0/1 knapsack, frog jump, max sum of non-adjacent elements, ninja training, and counting total unique paths in a grid. For each problem, recursion, memoization, and tabulation solutions are demonstrated.

Uploaded by

Raunak Bhalotia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views23 pages

DP 1

PDF (No Image Break) The document discusses various dynamic programming problems and their solutions. It explains the three steps of solving a DP problem: 1) the recursion equation, 2) memoization by storing values in an array or vector, and 3) tabulation using a bottom-up approach. Several DP problems are then covered, including Fibonacci numbers, 0/1 knapsack, frog jump, max sum of non-adjacent elements, ninja training, and counting total unique paths in a grid. For each problem, recursion, memoization, and tabulation solutions are demonstrated.

Uploaded by

Raunak Bhalotia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
-->There are three steps in solving a dp problem: PDF (No Image Break)
1st is the recursion equation.
2nd is the memoization that is storing the already found values in vector or
array anad using them when needed again.
3rd is the tabulation that is the bottom up approach.

1.) Fibonacci sequence(striver soln)

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 1/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 2/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 3/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

0/1 knapsack(Aditya verma):


--> we have to fill a knapsack so that its value is maximum and each element
can be included only once.
06:16

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 4/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

recursive approach:

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 5/23
10/30/22, 2:58 PM Askify | View Notes

memoization approach:
// we make matrix of all those variable whose values are changing
// initialise all the values of the array with -1 using memset (dp, -1, sizeof(t)) PDF (Fast)
// before calling the recursive function we are checking if the values is not -1
PDF (No Image Break)
if not we'll return the values else we will call the function and we will store
its value in the dp matrix
// here the changing variables are n and w

tabulaiton or bottom-up approach


// using bottomm up approch
// here in the loop i represents n which is the size of the array and the j in the
loop represent the val of array element

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 6/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

Frog jump(striver):
memoization:

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 7/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

tabulaiton:

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 8/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

-->can optimize space by not taking a dp array but by initialising two


variables prev1 and prev2 and storing min value in curi variable and after
each iteration change curri to prev and so on and at the end return prev.
max-sum of non adjacent elements(striver):

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 9/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

tabulation not included


ninja training(striver):
memoization:

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 10/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 11/23
10/30/22, 2:58 PM Askify | View Notes

tabulation:

PDF (Fast)
PDF (No Image Break)

tabulation with space optimization:


https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 12/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

2D DP(striver):
https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 13/23
10/30/22, 2:58 PM Askify | View Notes

total unique paths(striver):memoization code:


PDF (Fast)
PDF (No Image Break)

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 14/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 15/23
10/30/22, 2:58 PM Askify | View Notes

tabulation code:
PDF (Fast)
PDF (No Image Break)

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 16/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 17/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
space optimized code:
PDF (No Image Break)

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 18/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 19/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

minimum sum path(striver):


memoization code:

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 20/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

tabulation code:

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 21/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)
PDF (No Image Break)

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 22/23
10/30/22, 2:58 PM Askify | View Notes

PDF (Fast)

PDF (No Image Break)

Now you can use Askify in any websites

See How

https://askify.video/view/youtube?_id=45c956cc-0174-42ac-aefe-a74684c0cb50 23/23

You might also like