"""Longest strictly increasing subsequence (LIS) in O(n log n).

Idea
----
Keep ``tails[k]`` = the smallest possible last value of an increasing
subsequence of length ``k + 1``. ``tails`` is always sorted, so for each new
value binary-search the first tail >= value and replace it (or append if the
value is larger than every tail). The answer is ``len(tails)``.

``tails`` is *not* itself a valid subsequence; it is bookkeeping. The simpler
O(n^2) DP (``dp[i] = 1 + max(dp[j] for j < i if a[j] < a[i])``) is worth
knowing too.

Complexity
----------
Time:  O(n log n)
Space: O(n)
"""

from bisect import bisect_left
from collections.abc import Sequence


def lis_length(values: Sequence[int]) -> int:
    """Return the length of the longest strictly increasing subsequence."""
    tails: list[int] = []
    for v in values:
        i = bisect_left(tails, v)
        if i == len(tails):
            tails.append(v)
        else:
            tails[i] = v
    return len(tails)
