Skip to content

Some new dp problems added #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Dynamic_Programming/Longest_Increasing_Subsequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'''
Task : Given an integer array nums, return the length of the longest strictly increasing subsequence
Leetcode problem 300.
'''

def solve(nums) :
dp = [1]*len(nums)
for i in range(len(nums)) :
for j in range(i) :
if nums[j]<nums[i] : dp[i] = max(dp[i], dp[j]+1)
return max(dp)

def inlt():
return(list(map(int,input().split())))

if __name__=='__main__' :
print('Longest Increasing Subsequence Subroutine')
print('Input Space Separated Integers')
a = inlt()
ans = solve(a)
print(ans)
29 changes: 29 additions & 0 deletions Dynamic_Programming/Max_Product_Subarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'''
Task : Given an integer array nums, find a subarray that has the largest product, and return the product.
Leetcode problem 152.
'''

def maxProduct(nums) :
if len(nums)==1 : return nums[0]
if len(nums)==2 : return max(nums[0]*nums[1], max(nums))

# store the largest and smallest pdt ending at each index
mx = nums[0]
mn = nums[0]
ans = nums[0]
for num in nums[1:] :
temp = max(num, mx*num, mn*num) # 6
mn = min(num, mx*num, mn*num) # -3
mx = temp
ans = max(ans, mx) # 6
return ans

def inlt():
return(list(map(int,input().split())))

if __name__=='__main__' :
print('Max Product Subarray Subroutine')
print('Input Space Separated Integers')
a = inlt()
ans = maxProduct(a)
print(ans)
51 changes: 1 addition & 50 deletions Dynamic_Programming/README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1 @@
# Longest Common Subsequence

The longest common subsequence problem is finding the longest sequence
which exists in both the given strings.

Subsequence
Let us consider a sequence S = <s1, s2, s3, s4, …,sn>.

A sequence Z = <z1, z2, z3, z4, …,zm> over S is called a subsequence of S,
if and only if it can be derived from S deletion of some elements.

Common Subsequence
Suppose, X and Y are two sequences over a finite set of elements.
We can say that Z is a common subsequence of X and Y, if Z is a subsequence of both X and Y.

Longest Common Subsequence
If a set of sequences are given, the longest common subsequence problem is to
find a common subsequence of all the sequences that is of maximal length.

The longest common subsequence problem is a classic computer science problem,
the basis of data comparison programs such as the diff-utility, and has applications
in bioinformatics. It is also widely used by revision control systems, such as SVN
and Git, for reconciling multiple changes made to a revision-controlled collection of files.

Naïve Method
Let X be a sequence of length m and Y a sequence of length n. Check for every subsequence
of X whether it is a subsequence of Y, and return the longest common subsequence found.

There are 2m subsequences of X. Testing sequences whether or not it is a subsequence of Y
takes O(n) time. Thus, the naïve algorithm would take O(n2m) time.

Dynamic Programming
Let X = < x1, x2, x3,…, xm > and Y = < y1, y2, y3,…, yn > be the sequences.
To compute the length of an element the following algorithm is used.

In this procedure, table C[m, n] is computed in row major order and
another table B[m,n] is computed to construct optimal solution.

# Example
In this example, we have two strings X = BACDB and Y = BDCB to find the longest common subsequence.

Following the algorithm LCS-Length-Table-Formulation (as stated above), we have calculated table C
(shown on the left hand side) and table B (shown on the right hand side).

In table B, we are using ‘D’, ‘L’ and ‘U’ for diagonal, left and up
respectively. After generating table B, the LCS is determined by function LCS-Print. The result is BCB.

As can be seen in the image below on the left, we are recurring from bottom to top to get the desired subsequence

![alt text](https://www.tutorialspoint.com/design_and_analysis_of_algorithms/images/lcs.jpg)
Files containing solutions to some basic problems on Dynamic Programming.