Skip to content

Commit 3309654

Browse files
committed
Amazon Flipkart GreyOrange Microsoft Mobicip Morgan-Stanley Snapdeal Visa Payu
1 parent d27ee61 commit 3309654

File tree

3 files changed

+61
-41
lines changed

3 files changed

+61
-41
lines changed

0-1Knapsack.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#Complexity O(nW)
2+
def knapsack(W, wt, val, n):
3+
dp = [[0 for i in range(W+1)] for i in range(n+1)]
4+
5+
for i in range(n+1):
6+
for j in range(W+1):
7+
if i == 0 or j == 0:
8+
dp[i][j] = 0
9+
elif j < wt[i-1]:
10+
dp[i][j] = dp[i-1][j]
11+
else:
12+
dp[i][j] = max(val[i-1] + dp[i-1][j-wt[i-1]], dp[i-1][j])
13+
return dp[n][W]
14+
val = [60, 100, 120]
15+
wt = [10, 20, 30]
16+
W = 50
17+
n = len(val)
18+
print knapsack(W, wt, val, n)

interleavingString.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,30 @@ def isInterleaved(a,b,c):
2222
print c + "is Interleaved of "+ a + " and " + b
2323
else:
2424
print "Not Interleaved"
25+
26+
27+
#Dynamic Approach
28+
def isInterleaved(a,b,c):
29+
m = len(a)
30+
n = len(b)
31+
32+
table = [[False for i in range(n+1)] for i in range(m+1)]
33+
34+
if m+n != len(c):
35+
return False
36+
37+
for i in range(m+1):
38+
for j in range(n+1):
39+
if i == 0 and j == 0:
40+
table[i][j] = True
41+
elif i == 0 and (b[j-1] == c[j-1]):
42+
table[i][j] = table[i][j-1]
43+
elif j == 0 and a[i-1] == c[i-1]:
44+
table[i][j] = table[i-1][j]
45+
elif a[i-1] == c[i+j-1] and b[j-1] != c[i+j-1]:
46+
table[i][j] = table[i-1][j]
47+
elif a[i-1] != c[i+j-1] and b[j-1] == c[i+j-1]:
48+
table[i][j] = table[i][j-1]
49+
elif a[i-1] == c[i+j-1] and b[j-1] == c[i+j-1]:
50+
table[i][j] = table[i-1][j] or table[i][j-1]
51+
return table[m][n]

lengthOfLongestSubsequence.py

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,19 @@
1-
def lbs(arr):
2-
n = len(arr)
3-
lis = [1 for i in range(n+1)]
1+
def lcs(s1, s2):
2+
m = len(s1)
3+
n = len(s2)
44

5-
for i in range(1,n):
6-
for j in range(0, i):
7-
if (arr[i] > arr[j]) and lis[i] < lis[j] + 1:
8-
lis[i] = lis[j]+1
9-
lds = [1 for i in range(n+1)]
5+
dp = [[None]*(n+1) for i in range(m+1)]
106

11-
for i in reversed(range(n-1)):#loop from n-2 to 0
12-
for j in reversed(range(i-1,n)):#loop from n-1 downto i-1
13-
if arr[i] > arr[j] and lds[i] < lds[j]+1:
14-
lds[i] = lds[j]+1
7+
for i in range(m+1):
8+
for j in range(n+1):
9+
if i == 0 or j == 0:
10+
dp[i][j] = 0
11+
elif s1[i-1] == s2[j-1]:
12+
dp[i][j] = dp[i-1][j-1] + 1
13+
else:
14+
dp[i][j] = max(dp[i-1][j],dp[i][j-1])
1515

16-
maximum = lis[0] + lds[0] - 1
17-
for i in range(1, n):
18-
maximum = max((lis[i]+lds[i]-1),maximum)
19-
return maximum
20-
21-
arr = [ 64, 263, 11, 188, 396, 268, 116, 453, 207, 39, 312, 401, 396, 476, 56, 277, 104, 213, 79, 128, 36, 393, 117, 298, 371, 73, 110, -3, 350, 144, 210, 491, 312, 351, 183, 25, 230, 303, 478, 476, 276, 127, 184, 390, 481, 63, 270, 417, 257, 189, 18, 53, 80, 122, 358, 469, 244, 144, 152, 83, 188, 174, 381, 499, 280, 67, -1, 95, 489, 428, 302, 327, 25, 419, 207, 499, 457, 239, 12, 438, 54, 473, 116, 355, 428, -8, 37, 292, 116, 361, 186, 396, 375, -8, 416, 21, -8, 231, 51, 271, 318, 214, 361, 196, 118, 434, 133, 366, 192, 244, 400, 88, 444, 251, 165, 286, 15, 0, 223, 74, 84, 456, 464, 50, 209, 137, 67, 74, 4, 397, 403, 156, 70, 39, 187, 483, 267, 187, 464, 361, 74, 398, 479, 107, 123, 358, 317, 209, 192, 157, 61, 437, 330, -2, 276, 418, 418, 286, 303, 335, 317, 394, 193, 252, 64, 71, 322, 397, 494, 480, 285, 25, 298, 227, 16, 382, 452, 75, 304, 172, 469, 334, 294, 181, 394, 163, 326, 169, 30, 39, 425, 105, 294, 354, 207, 314, 386, 494 ]
22-
print "Length of LBS is",lbs(arr)
23-
24-
#Another Approach
25-
class Solution:
26-
# @param A : tuple of integers
27-
# @return an integer
28-
def longestSubsequenceLength(self, A):
29-
m=0
30-
L=[1]*len(A)
31-
for i in range(1,len(A)):
32-
for j in range(i):
33-
if A[j]<A[i]:
34-
L[i]=max(L[i],L[j]+1)
35-
R=[1]*len(A)
36-
for i in range(len(A)-2,-1,-1):
37-
for j in range(len(A)-1,i,-1):
38-
if A[j]<A[i]:
39-
R[i]=max(R[i],R[j]+1)
40-
for i in range(len(A)):
41-
val=L[i]+R[i]-1
42-
if val>m:
43-
m=val
44-
return m
16+
return dp[m][n]
17+
x = "AGGTAB"
18+
y = "GXTXAYB"
19+
print "Length of LCS is ", lcs(x, y)

0 commit comments

Comments
 (0)