From a1ff67c79e0734122b944a65686a84032fc269d4 Mon Sep 17 00:00:00 2001 From: John Law Date: Wed, 30 Oct 2019 23:21:48 +0800 Subject: [PATCH 1/2] Update longest_increasing_subsequence_o(nlogn).py --- .../longest_increasing_subsequence_o(nlogn).py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/longest_increasing_subsequence_o(nlogn).py b/dynamic_programming/longest_increasing_subsequence_o(nlogn).py index f7b2c6915bcb..7513429d333a 100644 --- a/dynamic_programming/longest_increasing_subsequence_o(nlogn).py +++ b/dynamic_programming/longest_increasing_subsequence_o(nlogn).py @@ -4,18 +4,28 @@ # comments: This programme outputs the Longest Strictly Increasing Subsequence in O(NLogN) # Where N is the Number of elements in the list ############################# + def CeilIndex(v, l, r, key): while r - l > 1: - m = (l + r) / 2 + m = (l + r) // 2 if v[m] >= key: r = m else: l = m - return r def LongestIncreasingSubsequenceLength(v): + """ + >>> LongestIncreasingSubsequenceLength([2, 5, 3, 7, 11, 8, 10, 13, 6]) + 6 + >>> LongestIncreasingSubsequenceLength([]) + 0 + >>> LongestIncreasingSubsequenceLength([0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]) + 6 + >>> LongestIncreasingSubsequenceLength([5, 4, 3, 2, 1]) + 1 + """ if len(v) == 0: return 0 @@ -37,5 +47,5 @@ def LongestIncreasingSubsequenceLength(v): if __name__ == "__main__": - v = [2, 5, 3, 7, 11, 8, 10, 13, 6] - print(LongestIncreasingSubsequenceLength(v)) + import doctest + doctest.testmod() From d49a97bb44abb9f91e1cab6c592d237280cc22f4 Mon Sep 17 00:00:00 2001 From: John Law Date: Wed, 30 Oct 2019 23:23:48 +0800 Subject: [PATCH 2/2] Update longest_increasing_subsequence_o(nlogn).py --- dynamic_programming/longest_increasing_subsequence_o(nlogn).py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/longest_increasing_subsequence_o(nlogn).py b/dynamic_programming/longest_increasing_subsequence_o(nlogn).py index 7513429d333a..4b06e0d885f2 100644 --- a/dynamic_programming/longest_increasing_subsequence_o(nlogn).py +++ b/dynamic_programming/longest_increasing_subsequence_o(nlogn).py @@ -4,6 +4,7 @@ # comments: This programme outputs the Longest Strictly Increasing Subsequence in O(NLogN) # Where N is the Number of elements in the list ############################# +from typing import List def CeilIndex(v, l, r, key): while r - l > 1: @@ -15,7 +16,7 @@ def CeilIndex(v, l, r, key): return r -def LongestIncreasingSubsequenceLength(v): +def LongestIncreasingSubsequenceLength(v: List[int]) -> int: """ >>> LongestIncreasingSubsequenceLength([2, 5, 3, 7, 11, 8, 10, 13, 6]) 6