mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 01:00:15 +00:00
07e991d553
* ci(pre-commit): Add pep8-naming to `pre-commit` hooks (#7038) * refactor: Fix naming conventions (#7038) * Update arithmetic_analysis/lu_decomposition.py Co-authored-by: Christian Clauss <cclauss@me.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor(lu_decomposition): Replace `NDArray` with `ArrayLike` (#7038) * chore: Fix naming conventions in doctests (#7038) * fix: Temporarily disable project euler problem 104 (#7069) * chore: Fix naming conventions in doctests (#7038) Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
#############################
|
|
# Author: Aravind Kashyap
|
|
# File: lis.py
|
|
# comments: This programme outputs the Longest Strictly Increasing Subsequence in
|
|
# O(NLogN) Where N is the Number of elements in the list
|
|
#############################
|
|
from __future__ import annotations
|
|
|
|
|
|
def ceil_index(v, l, r, key): # noqa: E741
|
|
while r - l > 1:
|
|
m = (l + r) // 2
|
|
if v[m] >= key:
|
|
r = m
|
|
else:
|
|
l = m # noqa: E741
|
|
return r
|
|
|
|
|
|
def longest_increasing_subsequence_length(v: list[int]) -> int:
|
|
"""
|
|
>>> longest_increasing_subsequence_length([2, 5, 3, 7, 11, 8, 10, 13, 6])
|
|
6
|
|
>>> longest_increasing_subsequence_length([])
|
|
0
|
|
>>> longest_increasing_subsequence_length([0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13,
|
|
... 3, 11, 7, 15])
|
|
6
|
|
>>> longest_increasing_subsequence_length([5, 4, 3, 2, 1])
|
|
1
|
|
"""
|
|
if len(v) == 0:
|
|
return 0
|
|
|
|
tail = [0] * len(v)
|
|
length = 1
|
|
|
|
tail[0] = v[0]
|
|
|
|
for i in range(1, len(v)):
|
|
if v[i] < tail[0]:
|
|
tail[0] = v[i]
|
|
elif v[i] > tail[length - 1]:
|
|
tail[length] = v[i]
|
|
length += 1
|
|
else:
|
|
tail[ceil_index(tail, -1, length - 1, v[i])] = v[i]
|
|
|
|
return length
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|