Skip to content

Instantly share code, notes, and snippets.

@Crocmagnon
Last active October 21, 2019 20:51
Show Gist options
  • Save Crocmagnon/47833399cb082f47032a77d13c287c80 to your computer and use it in GitHub Desktop.
Save Crocmagnon/47833399cb082f47032a77d13c287c80 to your computer and use it in GitHub Desktop.
def sub_length(arr):
"""
Given an array of n integers, find the length of the longest increasing subsequence
:param arr: The array to parse
:return: the length of the longest increasing subsequence
"""
if not arr:
return 0
longest_streak = 1
current_streak = 1
highest = arr[0]
for item in arr[1:]:
if item >= highest:
current_streak += 1
else:
if current_streak > longest_streak:
longest_streak = current_streak
current_streak = 1
highest = item
if current_streak > longest_streak:
longest_streak = current_streak
return longest_streak
def main():
assert sub_length([1, 2, 3, 4]) == 4
assert sub_length([1, 2, 3, 4, 4]) == 5
assert sub_length([4, 3, 2, 1]) == 1
assert sub_length([1, 2, 3, 2]) == 3
assert sub_length([1, 5, 9, 54, 0]) == 4
assert sub_length([13, 35, 1, 5, 40, 86]) == 4
assert sub_length([13, 1, 5, 40, 86]) == 4
assert sub_length([-13, -35, -86, -40, -5, -1]) == 4
assert sub_length([]) == 0
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment