Last active
October 21, 2019 20:51
-
-
Save Crocmagnon/47833399cb082f47032a77d13c287c80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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