Given an integer array nums, find the longest increasing subsequence and return how many such sequences exist.
A subsequence is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements. The sequence must be strictly increasing.
Example: In array [1, 3, 5, 4, 7], there are two longest increasing subsequences of length 4: [1, 3, 5, 7] and [1, 3, 4, 7], so the answer is 2.
This problem combines the classic Longest Increasing Subsequence challenge with a counting twist - you need to track not just the length, but also how many ways you can achieve that optimal length.
Input & Output
Visualization
Time & Space Complexity
O(n log n) for sorting and coordinate compression, O(n log n) for segment tree operations
Space for coordinate mapping and segment tree structure
Constraints
- 1 โค nums.length โค 2000
- -106 โค nums[i] โค 106
- The subsequence must be strictly increasing