Find Maximum Strictly Ascending Subsequence Length
Dynamic ProgrammingAuto-graded · 20 test casesLast updated
Tests dynamic programming optimization using binary search to efficiently compute the longest strictly increasing subsequence in O(n log n) time.
You are given an array of integers. Your task is to determine the length of the longest subsequence where each element is strictly greater than the one before it.
A subsequence is formed by removing zero or more elements from the array while keeping the relative order of the remaining elements unchanged. For instance, [5, 8, 12] is a valid subsequence of [5, 3, 8, 1, 12].
Write a function that returns the length of the longest such strictly ascending subsequence.
Constraints
1 <= nums.length <= 2500-10^4 <= nums[i] <= 10^4- Elements in the subsequence must be strictly increasing (no equal consecutive values allowed)
Examples
Example 1
Input: longest_ascending_length([10, 9, 2, 5, 3, 7, 101, 18])
Output: 4
Example 2
Input: longest_ascending_length([0, 1, 0, 3, 2, 3])
Output: 4
Hints
Hint 1
Start by thinking about a dynamic programming solution where you track the best subsequence ending at each position. What's the recurrence relation?
Hint 2
To optimize beyond O(n²), consider maintaining an auxiliary array where each position tracks the smallest possible ending value for subsequences of different lengths.
Hint 3
Since you're looking for positions in a sorted structure, binary search can reduce the time complexity to O(n log n).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is to maintain an array that tracks the smallest possible ending value for strictly increasing subsequences of each achievable length.
Specifically, we build an array tails where tails[k] represents the smallest ending element among all strictly increasing subsequences of length k + 1 that we've encountered so far. Keeping the smallest value is optimal because it maximizes opportunities for future elements to extend that subsequence.
For each new element in the input:
- If it's larger than all current tail values, it extends the longest subsequence found so far, so we append it
- Otherwise, we use binary search to locate the first tail value that is greater than or equal to the new element, and replace it. This updates our record with a better (smaller) tail for that subsequence length
The key property is that tails remains sorted throughout the process, enabling efficient binary search. We use bisect_left specifically to handle the strict inequality requirement—when a value equals an existing tail, we find and overwrite that position, preventing equal values from extending subsequences.
Correctness
Two invariants ensure correctness:
-
Sorted structure: The
tailsarray stays strictly increasing because when we place a value at positioni, it's bounded bytails[i-1] < value <= old_tails[i] -
Optimality: Each
tails[k]truly is the minimum possible tail for lengthk+1, because whenever we find a better (smaller) tail value, we immediately update it
The length of tails equals the answer because we add a new entry exactly when we achieve a new maximum subsequence length for the first time.
Complexity
- Time: O(n log n) — we process n elements, each requiring a binary search over at most n entries plus constant-time updates
- Space: O(L) where L is the result length, which is O(n) worst case (when the input is already sorted)
The alternative quadratic dynamic programming approach uses O(n) space and O(n²) time, which works for n ≤ 2500 but doesn't scale to larger inputs.
Implementation
from bisect import bisect_left
from typing import List
def longest_ascending_length(nums: List[int]) -> int:
tails = []
for value in nums:
i = bisect_left(tails, value)
if i == len(tails):
tails.append(value)
else:
tails[i] = value
return len(tails)