Dynamic Programming: Coding Competition

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Coding Competition

DYNAMIC PROGRAMMING

Students will be able to learn…..

1. What is Dynamic Programming?


2. What kind of question solve using it?
3. Simple problem to solve it using Dynamic
Programming.
Fibonacci Number: You want to figure out n number of
Fibonacci series. (1,1,2,3,5,8,13……..)
F[1]=1
F[n, n>2] = F[n-1] +F[n-2] //Recursive Definition
F[2]=1
Recursive tree:- Depth

To solve this problem, you have to


compute every node of this problem N=7
How much time required to solve this problem:

•How many number of states here.

•Depth of the tree.

•Fareast distance of tree from root node is 7 (depth of tree)

For n number of nodes, depth would be “n”


n
Number of states: O( )
2 //Refer the masters theorem of decreasing function

Time to compute each state : O( 1 )

Time Complexity : O( 2 n) //Recurrence relation is, T(n) = T(n-1) + T(n-2) + C


Observation:
Lots of states, we are computing, that could have been avoided.

Its no longer binary tree is here,


it’s a list now
Number of New states : O( N )
Idea:
You could have actually store the value of these states, and
whenever you wanted to get those value again rather then
recomputing them you could just utilize the answer that you
already have.
“We have reduce the problem into lesser number of states from more number of states ”

O( 2^n ) > O( N )

Lets see how this idea can help us to solve lots of problems.
Dynamic Programming:
Method of solving a complex problem by breaking it down
into a collection of simpler sub problems.
Solving each of these sub problems just once and storing
their solutions.
Fibonacci Number:

Recursion + Memoization

Time Complexity: O( N )
Dynamic Programming:
Features:
1. Optimal Substructure
2. Overlapping subproblem

Optimal solution to a bigger problem You can see, there is some sub
can actually be constructed using problems being computed multiple
optimal solutions to smaller problem. number of times.
Dynamic Programming Problems:
• Climbing Stairs Problem
Amazon, Intel, Morgan Stanley, LinkedIn, Snapdeal

There is a staircase of N steps and you can climb either 1 or 2 steps at a


time. You need to count and return the total number of unique ways to
climb the staircase. The order of steps taken matters.
# of ways to reach Nth stair from the 1st stair

n Case 1:

# of ways=1 Case 3:

II
I
Case 2: # of ways=2
ith (i+1)th

ith (i+2)th # of ways=1


Let, N=5 # of Ways(V) = ?
V
IV
# of Ways(IV) = A
III
# of Ways(III) = B IV V III V
II
I
A×1 B ×1

# of Ways(V) = # of Ways(IV) × 1 + # of Ways(III) × 1

Ways(N) = Ways( n - 1) × 1 + Ways(n - 2) × 1

Ways(N) = Ways( n - 1) + Ways(n - 2)


Dynamic Programming:
Methods:
1. Top-down approach
Recursive + Memoization

2. Bottom-up approach ( Tabulation Method)


Base case(s)
F[1]=1
F[3]=2
F[2]=1
F[1] F[2] F[3] F[4]

F[4]=3
F[5]=5
F[6]=8
N=8

i c Return P
0 1 c=n=1 1
1 2 c=n=2 1
2 3 Fib(2)+Fib(1) 2
1+1
3 4 Fib(3)+Fib(2) 3
{{Fib(2)+Fib(1)}+Fib(2)}
{{1+1}+1}

7 8 Fib(7)+Fib(6) 21
{{Fib(6)+Fib(5)}+
{Fib(5)+Fib(4) }}

{13+8}
N=8

i c F[ 0,1,2,3,4,5,6,7,8] P
0 1 0 1 -1 -1 -1 -1 -1 -1 1
1 2 0 1 -1 -1 -1 -1 -1 -1 1
2 3 0 1 1 -1 -1 -1 -1 -1 2
3 4 0 1 1 2 -1 -1 -1 -1 3
References
1. https://www.youtube.com/watch?v=gwaGGkRfcOg&list=PLLhBy6YSIT0Dg3Gl
nTQ_AtBRYNyZBQDQP&index=1

2. https://www.sanfoundry.com/dynamic-programming-problems-solutions/

3. https://www.codesdope.com/course/algorithms-dynamic-programming/

You might also like