Number of Longest Increasing Subsequence

Pattern: LIS family

medium

Alongside len[i] (the longest run ending at i), keep count[i], how many runs reach that length. A longer way to i resets the count; an equally long one adds to it. Sum the counts of every index with the overall longest length.

The problem

Return how many longest strictly increasing subsequences nums has (counted by positions).

Think first

  • What extra number per index is needed?
  • When does count[i] reset?
  • When does it grow?

Hints

    Brute force, and why it fails

    Generate every increasing subsequence: exponential.

    The key insight

    Counts can ride along with lengths in the same O(n²) DP.

    Dry run

    [1, 3, 5, 4, 7]. len: 1 2 3 3 4. count: 1 1 1 1 2.

    Step 1 of 2

    The plan

    for i, j < i with a[j] < a[i]:
      if len[j] + 1 > len[i]: len[i] = len[j] + 1; count[i] = count[j]
      elif equal: count[i] += count[j]
    sum count[i] where len[i] is the longest
    

    Complexity

    Time
    O(n²)
    Space
    O(n)

    Every pair once.

    Edge cases

    • All equal: n (each value alone).
    • Sorted: 1.
    • One value: 1.
    I've built my logic, show the code

    Java

    class Solution {
        public int findNumberOfLIS(int[] nums) {
            int n = nums.length, longest = 0, total = 0;
            int[] len = new int[n], count = new int[n];   // the longest run ending at i, and how many runs have that length
            for (int i = 0; i < n; i++) {
                len[i] = 1;
                count[i] = 1;
                for (int j = 0; j < i; j++) {
                    if (nums[j] >= nums[i]) continue;
                    if (len[j] + 1 > len[i]) { len[i] = len[j] + 1; count[i] = count[j]; }   // a longer run: start counting again
                    else if (len[j] + 1 == len[i]) count[i] += count[j];                     // another way to the same length
                }
                if (len[i] > longest) { longest = len[i]; total = count[i]; }
                else if (len[i] == longest) total += count[i];
            }
            return total;
        }
    }

    C++

    class Solution {
    public:
        int findNumberOfLIS(vector<int>& nums) {
            int n = static_cast<int>(nums.size()), longest = 0, total = 0;
            vector<int> len(n, 1), count(n, 1);     // the longest run ending at i, and how many runs have that length
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < i; j++) {
                    if (nums[j] >= nums[i]) continue;
                    if (len[j] + 1 > len[i]) { len[i] = len[j] + 1; count[i] = count[j]; }   // a longer run: start counting again
                    else if (len[j] + 1 == len[i]) count[i] += count[j];                     // another way to the same length
                }
                if (len[i] > longest) { longest = len[i]; total = count[i]; }
                else if (len[i] == longest) total += count[i];
            }
            return total;
        }
    };

    Python

    class Solution:
        def findNumberOfLIS(self, nums: list[int]) -> int:
            n = len(nums)
            length, count = [1] * n, [1] * n        # the longest run ending at i, and how many runs have that length
            for i in range(n):
                for j in range(i):
                    if nums[j] < nums[i]:
                        if length[j] + 1 > length[i]:
                            length[i], count[i] = length[j] + 1, count[j]   # a longer run: start counting again
                        elif length[j] + 1 == length[i]:
                            count[i] += count[j]    # another way to the same length
            longest = max(length)
            return sum(c for l, c in zip(length, count) if l == longest)

    Common mistakes

    • Adding counts when the length is shorter.
    • Returning only count at the last index.

    Similar problems