Skip to content

Instantly share code, notes, and snippets.

@maxlikely
Created February 22, 2012 20:24
Show Gist options
  • Save maxlikely/1887000 to your computer and use it in GitHub Desktop.
Save maxlikely/1887000 to your computer and use it in GitHub Desktop.
Longest Oscillating Sub-sequence Naive
def los(A,k):
"""Auxiliary function for additional parameter k"""
if len(A) < 2: return 0
plusOrMinus = lambda x, y: abs(x, y) < 2
# don't take element 0
m = las(A[1:], k)
# take element 0
if plusOrMinus(A[0], k): m = max(m, 1 + las(A[1:], A[0]))
return m
def longest_oscillating_subsequence(A):
"""Naive approach to find longest increasing sub-sequence s.t. every
element is +/-1 of previous"""
if not A: return 0
return los(A,A[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment