Skip to content

Instantly share code, notes, and snippets.

@tmendozas
tmendozas / dp_lbs.py
Created August 12, 2018 02:11
DP - Longest Bitonic Subsequence
# Given an array find the length of the longest sequence that is
# first increasing and then decreasing
# A = {1, 11, 2, 10, 4, 5, 2, 1} -> 6
# A = {80, 60, 30, 40, 20, 10} -> 5
def find_increasing_to_extend(tails, element):
# Use binary search, as they always increase
low = 0
up = len(tails)
@tmendozas
tmendozas / dp_lcs.py
Created August 8, 2018 22:42
DP - Longest Common Subsequence
def lcs(x,y):
n = len(x)
m = len(y)
longest = 0
# each element table[i][j] saves the lcs for x cropped to i chars and y to j chars
table = [[0]*m for i in range(n)]
for i in range(n):
for j in range(m):
if x[i] == y[j]:
#we found a common element
@tmendozas
tmendozas / dp_lis.py
Last active August 8, 2018 20:59
DP - Longest Increasing Subsequence
def find_seq_to_extend(num, tail_by_size):
# tail_by_sizes is monotonically ascending
# -> Use binary search
l = 0
r = len(tail_by_size)
while l<r:
mid = (l+r)/2
if tail_by_size[mid] >= num:
# keep considering this elem, and elements smaller
r = mid
def min_steps(numbers):
initial = numbers[0]
residual = initial%2
minimum = initial
maximum = initial
for num in numbers:
if num%2 != residual:
return -1
minimum = num if num < minimum else minimum
maximum = num if num > maximum else maximum

Keybase proof

I hereby claim:

  • I am tmendozas on github.
  • I am tmendozas (https://keybase.io/tmendozas) on keybase.
  • I have a public key ASASnHsDBLV_URMU1o1ZBcDavPy42OhZ0amifQuVu9DSowo

To claim this, I am signing this object: