Sub topic 11
Counting All Longest Increasing Subsequences
This chapter assumes you know the O(n^2) dynamic program for LIS length, where dp[i] is the length of the longest strictly increasing subsequence ending at index i. It also assumes you can read nested Python loops. Here the goal changes from finding the length of the LIS to finding how many different subsequences reach that length.
What Counts as "Different"
Two subsequences are different when they use different sets of indices, even if their values match. For [2, 2], the strictly increasing LIS has length 1. There are two of them: index 0 alone and index 1 alone. This matches the usual definition in interview problems and judges, and it falls out of the DP below without extra work.
Extending the State
The length DP keeps one number per index. To count, keep two numbers:
length[i]is the length of the longest increasing subsequence ending ati.count[i]is how many increasing subsequences ending atihave exactly that length.
Each index starts with length[i] = 1 and count[i] = 1, because the element on its own is a subsequence. For every earlier j with a[j] < a[i], appending a[i] to a best subsequence ending at j gives a candidate of length length[j] + 1, and there are count[j] such candidates. Three cases follow:
| Comparison | Action |
|---|---|
| `length[j] + 1 > length[i]` | A new best length. Set `length[i] = length[j] + 1` and **replace** `count[i]` with `count[j]` |
| `length[j] + 1 == length[i]` | Another way to reach the same best length. **Add** `count[j]` to `count[i]` |
| `length[j] + 1 < length[i]` | Shorter than the best, so ignore it |
The answer is the sum of count[i] over every index whose length[i] equals the overall maximum. An LIS can end at any index, so you cannot read the answer from a single position.
The Implementation
def count_lis(a):
n = len(a)
if n == 0:
return 0
length = [1] * n
count = [1] * n
for i in range(n):
for j in range(i):
if a[j] < a[i]:
if length[j] + 1 > length[i]:
length[i] = length[j] + 1
count[i] = count[j]
elif length[j] + 1 == length[i]:
count[i] += count[j]
best = max(length)
return sum(c for l, c in zip(length, count) if l == best)
print(count_lis([1, 3, 5, 4, 7])) # 2The function runs in O(n^2) time and uses O(n) space, the same as the length-only DP.
A Worked Trace
Here is the trace for [1, 3, 5, 4, 7]:
| i | a[i] | length[i] | count[i] | Reason |
|---|---|---|---|---|
| 0 | 1 | 1 | 1 | Element on its own |
| 1 | 3 | 2 | 1 | Extends 1 |
| 2 | 5 | 3 | 1 | Extends 3 |
| 3 | 4 | 3 | 1 | Extends 3 (5 is not less than 4) |
| 4 | 7 | 4 | 2 | Extends 5 (count 1), then 4 ties (+1) |
The maximum length is 4, and only index 4 reaches it, so the answer is 2. The two subsequences are [1, 3, 5, 7] and [1, 3, 4, 7].
Common Errors
- Adding 1 instead of
count[j]. Each of thecount[j]subsequences ending atjcan be extended, so you must add all of them. - Adding on a strictly longer length. When a longer length appears, the old count belongs to a shorter length and is now wrong. Replace it instead of adding to it.
- Returning only
count[n-1]. The last element may not be part of any LIS. - Using
<=in the comparison. That counts non-decreasing subsequences. This is a valid variant, but it answers a different question. - Overflow. The count can grow exponentially in
n. Python integers handle this, but in fixed-width languages problems usually ask for the answer modulo a prime.
Exercises
Exercise
What does count_lis return for [2, 2, 2, 2, 2], and why?
Show answer
It returns 5. No element is strictly less than another, so every length[i] = 1 and count[i] = 1. The maximum length is 1, and summing over all five indices gives 5 distinct single-index subsequences.
Exercise
For [1, 2, 4, 3, 5, 4, 7, 2], give the LIS length and the number of LIS.
Show answer
The length is 5 and there are 3 of them: [1,2,4,5,7], [1,2,3,5,7], and [1,2,3,4,7]. The DP gives index 4 (value 5) length 4 with count 2 and index 5 (value 4) length 4 with count 1. Index 6 (value 7) then gets length 5 with count 2 + 1 = 3.
Exercise
Why must count[i] be replaced, not incremented, when length[j] + 1 > length[i]?
Show answer
The previous count[i] counted subsequences of the old, shorter best length. Once a longer length is found, those subsequences are no longer the longest ending at i, so count[i] must restart from count[j].