You are on page 1of 6

DYNAMIC PROGRAMMING

It is technique mainly used to avoid the recursion. Because the recursion the
exponential time to get the result and we have to solve the things again even
though we have done already.
Dynamic programming is used when there is a overlapping of subproblems,
optimal substructure property.
Two types of approaches:
1) Memoization
2) Tabulation
Basically we store the results of these calculations and use whenever required.
Example:
Fibanocii sequence
Sum of Subsets Problem:
We have find whether there is a subset possible with the given target sum.
Using recursion: (Brute Force Approach)
SumofSubsets(array,n,target) = SumOfSubsets(array,n-1,target-arr[i]) or
SumOfSubsets(array,n-1,target)

Basic Conditions are:


If(target becomes zero) then return the True
If(n==0 and target>0) then return False
Using Dynamic Programming:
Create an two dimensional array (dp) of size n*target. The first row is
initialized with False and the first column is initialized with True.
Then we will run two loops. One loop (I) is over the array elements and
second loop (j) is over the target elements. Suppose if the element is greater than j
value then take dp[i-1][j]. Else take dp[i-1][j] or dp[i-1][j-arr[i-1]]. At last return
the dp[n][target].
Code:
#dynamic programming subset problem
n=int(input())
l=list(map(int,input().split()))
sum=int(input())
dp=[[False for i in range(0,sum+1)]for j in range(0,n+1)]
for i in range(0,n+1):
print(i)
dp[i][0]=True
for i in range(1,n+1):
for j in range(1,sum+1):
if(l[i-1]>j):
dp[i][j]=dp[i-1][j]
else:
dp[i][j]=dp[i-1][j] or dp[i-1][j-l[i-1]]
print(dp[n][sum])

Equal Subset Sum Parition:


The problem to divide the array into two sets and the sets have the same sum.
Firstly if we want divide into two sets with equal sum the total sum must be even.
If the total sum is odd we cannot partition it and return False. If there is possibility
of the even sum divide the sum//2 and apply Subset sum problem if it returns true
then there is a posibility of dividing.
Count of Subset Sum:
Just use the approach to solve the Subset sum and take count of values instead of
Boolean values.
Dp[i][j]=dp[i-1][j]+dp[i-1][j-arr[i-1]]
Minimum Subset Sum Difference:
Longest Common Subsequence in the string:
Given the two strings we have to find the longest common subsequence. The
subsequence is nothing but element can be taken from any place but in the correct
order.
Basically we consider an dp array of size (length_string1)*(length_string2) and
initialize all of them to zero.
Now the logic is given below:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
dp=[[0 for i in range(0,len(text2)+1)] for j in range(0,len(text1)+1)]
for i in range(1,len(text1)+1):
for j in range(1,len(text2)+1):
if(text1[i-1]==text2[j-1]):
dp[i][j]=1+dp[i-1][j-1]
else:
dp[i][j]=max(dp[i][j-1],dp[i-1][j])
return dp[len(text1)][len(text2)]
Longest Palindromic Subsequences:
Just we use the longest common subsequence. We will take the reverse of the
string as second string and first string is the given string and then we will find the
longest palindromic subsequence.
Longest Common Substring:
We take the two dimensional array of size n*m. The Code:
L,r=0,0
Result=0
For I in range(1,n+1):
For j in range(1,m+1):
If(s[i-1]==s1[j-1]):
Dp[i][j]=1+dp[i-1][j-1]
L,r=I,j
Result=max(Result,dp[i][j])
Result=[‘0’]*result
while(dp[l][r]!=0):
result-=1
Result[result]=s[l-1]
l-=1
r-=1
Minimum Deletions to Make to become the palindromic string is:
Length of the string – longest palindromic subsequence.
Count of all the Palindromic substrings:
Basically if equals then increment the count.

Longest Increasing Subsequence:


Here we take the single dimensional array of length “n” and we will use two loops.
First loop iterates through all the elements whereas the second loop iterates
through from 0 to I in that check if arr[i]>arr[j] but its dp[i] less than 1+dp[j] then
change the dp[i] value. After completion of loops return max(dp).
Coin Change Problem:
Given a number of coins in an array and number “n” then we have to count the
number of ways we can give.
Exclude the coin+ include the coin
Count(arr,m-1,n)+Count(arr,m-1,n-arr[m])
Base Conditions are:
If(n==0): 1
If(n<0): 0
If(m<=0 and n>=1): 0
Prime Numbers Check in Least Time:
If(n==1 or n<=1):
Return 0
Else if(n==2 or n==3):
Return 1
Else if(n%2==0 or n%3==0):
Return 0
I=5
While(i*i<=n):
If(n%i==0 or n%(i+2)==0):
Return 0
I+=6
Return 1

Trapping Rain Water:


L=list(map(int,input().split()))
1)Take the left and right array
2)left array:
Left[0]=l[0]
For I in range(1,n):
Left[i]=max(left[i-1],l[i])
Right[n-1]=l[-1]
For I in range(n-2,-1,-1):
Right[i]=max(right[i+1],l[i])
For I in range(0,n):
Result+=min(right[i],left[i])-arr[i]

You might also like