Longest increasing subsequence
Return the length of the longest strictly increasing subsequence of integers. The subsequence need not be contiguous.
from bisect import bisect_left
tails = [2, 5, 7]
position = bisect_left(tails, 3)
tails[position] = 3
print(tails)Reason about the state
Keep the smallest possible tail for each subsequence length. Lower-bound replacement prevents equal values from increasing the length, giving O(n log n) time. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the length of the longest strictly increasing subsequence of integers. The subsequence need not be contiguous.
The explanation opens here once every check passes.