Overview
This builds directly on the standard LIS O(N^2) solution. Instead of just maintaining an array `dp` for lengths, we introduce a second array `count`.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Not just finding the length of the Longest Increasing Subsequence, but finding HOW MANY subsequences have that maximum length.
This builds directly on the standard LIS O(N^2) solution. Instead of just maintaining an array `dp` for lengths, we introduce a second array `count`.
`count[i]` represents the number of longest increasing subsequences that strictly end at index `i`.
When extending `dp[j]`, if `dp[j] + 1 > dp[i]`, we found a strictly longer sequence. We update `dp[i]` and INHERIT the count from `j`: `count[i] = count[j]`.
If `dp[j] + 1 == dp[i]`, we found another distinct way to form a sequence of the current max length. We ADD the counts: `count[i] += count[j]`.