Number of Longest Increasing Subsequence - Problem

Given an integer array nums, return the number of longest increasing subsequences.

Notice that the sequence has to be strictly increasing.

A subsequence is a sequence derived from an array by deleting some or no elements without changing the order of the remaining elements.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,3,6,7,9,4,10,5,6]
Output: 2
💡 Note: The longest increasing subsequences are [1,3,6,7,9] and [1,3,6,7,10], both have length 5. So there are 2 longest increasing subsequences.
Example 2 — Single Element Repeated
$ Input: nums = [2,2,2,2,2]
Output: 5
💡 Note: Each single element forms a valid increasing subsequence of length 1. Since there are 5 elements, there are 5 longest increasing subsequences.
Example 3 — Strictly Decreasing
$ Input: nums = [5,4,3,2,1]
Output: 5
💡 Note: No increasing subsequence of length > 1 exists. Each single element is a longest increasing subsequence, giving us 5 subsequences of length 1.

Constraints

  • 1 ≤ nums.length ≤ 2000
  • -106 ≤ nums[i] ≤ 106

Visualization

Tap to expand
Number of Longest Increasing SubsequencesInput Array:1367941056Longest Subsequence 1:13679(Length: 5)Longest Subsequence 2:136710(Length: 5)Result: 2 longest increasing subsequences
Understanding the Visualization
1
Input
Array with elements that can form increasing subsequences
2
Process
Find all longest increasing subsequences
3
Output
Count how many longest subsequences exist
Key Takeaway
🎯 Key Insight: Use dynamic programming to simultaneously track the length and count of longest increasing subsequences ending at each position
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28
89.0K Views
Medium Frequency
~25 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen