Skip to content

Commit c1445d2

Browse files
Merge pull request matthewsamuel95#322 from shikharp16/lcspy
Added LCS code in Python
2 parents 5103790 + 5b8aa22 commit c1445d2

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

DP/LongestCommonSubsequence/LCS.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def lcs(X , Y):
2+
# find the length of the strings
3+
m = len(X)
4+
n = len(Y)
5+
6+
# declaring the array for storing the dp values
7+
L = [[None]*(n+1) for i in xrange(m+1)]
8+
9+
"""Following steps build L[m+1][n+1] in bottom up fashion
10+
Note: L[i][j] contains length of LCS of X[0..i-1]
11+
and Y[0..j-1]"""
12+
for i in range(m+1):
13+
for j in range(n+1):
14+
if i == 0 or j == 0 :
15+
L[i][j] = 0
16+
elif X[i-1] == Y[j-1]:
17+
L[i][j] = L[i-1][j-1]+1
18+
else:
19+
L[i][j] = max(L[i-1][j] , L[i][j-1])
20+
21+
# L[m][n] contains the length of LCS of X[0..n-1] & Y[0..m-1]
22+
return L[m][n]

Greedy/MinimumCoins/mincoins.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// All denominations of Indian Currency
5+
int deno[] = {1, 2, 5, 10, 20, 50, 100, 500, 1000};
6+
int n = sizeof(deno)/sizeof(deno[0]);
7+
8+
// Driver program
9+
void findMin(int V)
10+
{
11+
// Initialize result
12+
vector<int> ans;
13+
14+
// Traverse through all denomination
15+
for (int i=n-1; i>=0; i--)
16+
{
17+
// Find denominations
18+
while (V >= deno[i])
19+
{
20+
V -= deno[i];
21+
ans.push_back(deno[i]);
22+
}
23+
}
24+
25+
// Print result
26+
for (int i = 0; i < ans.size(); i++)
27+
cout << ans[i] << " ";
28+
}
29+
30+
// Driver program
31+
int main()
32+
{
33+
int n;
34+
cin>>n;
35+
cout << "Following is minimal number of change for " << n << " is ";
36+
findMin(n);
37+
return 0;

0 commit comments

Comments
 (0)