Skip to content
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

Bitonic #33

Open
wants to merge 2 commits 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
41 changes: 41 additions & 0 deletions LongestBitonicSequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
answer = []
def index_ceiling(arr,T, left,right, key):
while (right - left > 1):
mid = left + (right - left) // 2;
if (arr[T[mid]] >= key):
right = mid
else:
left = mid
return right
def long_inc_seq(A):
n = len(A)
tails_idx = [0]*(n)
prev_idx = [-1]*(n)
length = 1
for i in range(1, n):
if (A[i] < A[tails_idx[0]]):
tails_idx[0] = i
elif (A[i] > A[tails_idx[length - 1]]):
prev_idx[i] = tails_idx[length - 1]
tails_idx[length] = i
length += 1
else:
pos = index_ceiling(A, tails_idx, -1, length - 1, A[i])
prev_idx[i] = tails_idx[pos - 1]
tails_idx[pos] = i
i = tails_idx[length - 1]
while(i >= 0):
answer.append(A[i])
i = prev_idx[i]
def long_bitonic(A,B):
n1 = len(A)
n2 = len(B)
global answer
long_inc_seq(A)
answer = answer[::-1]
B = B[::-1]
long_inc_seq(B)
A = [2, 6, 3, 5, 4, 6]
B = [9, 7, 5, 8, 4, 3]
long_bitonic(A,B)
print(answer)
16 changes: 16 additions & 0 deletions LongestPalindromicSubsequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def longestPalindromeSubseq(self, s: str) -> int:
n = len(s)
dp = [[0]*(n) for _ in range(n) ]

for i in range(n):
dp[i][i] = 1


for i in range(n-1, -1, -1):
for j in range(i+1, n):
if s[i]==s[j]:
dp[i][j] = dp[i+1][j-1]+2
else:
dp[i][j] = max(dp[i][j-1], dp[i+1][j])

return dp[0][-1]