Doctest, type hints and bug in LIS_O(nlogn) (#1525)

* Update longest_increasing_subsequence_o(nlogn).py

* Update longest_increasing_subsequence_o(nlogn).py
This commit is contained in:
John Law 2019-10-31 01:06:07 +08:00 committed by GitHub
parent df95f43907
commit 357dbd4ada
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,18 +4,29 @@
# 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:
m = (l + r) / 2
m = (l + r) // 2
if v[m] >= key:
r = m
else:
l = m
return r
def LongestIncreasingSubsequenceLength(v):
def LongestIncreasingSubsequenceLength(v: List[int]) -> int:
"""
>>> 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 +48,5 @@ def LongestIncreasingSubsequenceLength(v):
if __name__ == "__main__":
v = [2, 5, 3, 7, 11, 8, 10, 13, 6]
print(LongestIncreasingSubsequenceLength(v))
import doctest
doctest.testmod()