Longest Strictly Increasing Subsequence
Dynamic ProgrammingAuto-graded · 24 test casesLast updated
Tests understanding of dynamic programming optimization using binary search and greedy subsequence construction with the patience sorting technique.
You are given an array of integers. Your task is to compute the length of the longest subsequence where each element is strictly greater than the previous one.
A subsequence maintains the relative order of elements from the original array but does not need to be contiguous. "Strictly greater" means equal values do not count as increasing.
Constraints
- 0 ≤ array length ≤ 2500
- -10⁴ ≤ array elements ≤ 10⁴
- An empty array has a longest increasing subsequence of length 0
- If all elements are equal, the answer is 1 (a single element)
Examples
Example 1
Input: lis_length([10, 9, 2, 5, 3, 7, 101, 18])
Output: 4
Example 2
Input: lis_length([0, 1, 0, 3, 2, 3])
Output: 4
Example 3
Input: lis_length([7, 7, 7, 7, 7, 7, 7])
Output: 1
Example 4
Input: lis_length([3, 4, -1, 0, 6, 2, 3])
Output: 4
Hints
Hint 1
A dynamic programming approach with O(n²) time is straightforward but can be improved
Hint 2
Consider maintaining information about the smallest ending value for subsequences of each possible length
Hint 3
Binary search can help you efficiently find where to place each new element
Hint 4
The bisect_left function enforces strict inequality; bisect_right would allow non-decreasing sequences
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The optimal solution uses a greedy strategy inspired by patience sorting. We maintain an auxiliary array where each position tracks the smallest possible ending value for an increasing subsequence of that length.
Specifically, we build a tails array where tails[i] stores the smallest tail element among all strictly increasing subsequences of length i+1 discovered so far.
For each number in the input:
- Use binary search to find the leftmost position where the current number could be placed
- If the number is larger than all existing tails, it extends our longest subsequence, so append it
- Otherwise, replace the tail at the found position with this smaller value, improving future extension possibilities
The key insight is that tails remains sorted throughout execution. When we encounter a value, the number of tails strictly smaller than it equals the length of the longest subsequence we can extend. Binary search finds this position in O(log n) time.
Why bisect_left matters: Using bisect_left (not bisect_right) ensures we replace equal values rather than inserting after them, enforcing strict inequality.
Correctness
The tails array maintains this invariant: if a subsequence of length k+1 exists ending at value t, then tails[k] ≤ t. This means len(tails) correctly represents the maximum achievable length.
The array stays sorted because any length-k+1 subsequence must build on a length-k subsequence with a smaller tail. Therefore each tail is strictly less than the next.
Complexity
Time: O(n log n) where n is the array length. We process each element once and perform a binary search over at most n positions.
Space: O(n) worst case when the entire input is already increasing. More precisely, space is O(L) where L is the answer.
Implementation
from bisect import bisect_left
from typing import List
def lis_length(nums: List[int]) -> int:
tails = []
for value in nums:
index = bisect_left(tails, value)
if index == len(tails):
tails.append(value)
else:
tails[index] = value
return len(tails)