Given an integer array nums, return the number of longest increasing subsequences.
Notice that the sequence has to be strictly increasing.
Input: nums = [1,3,5,4,7]
Output: 2
Explanation: The two longest increasing subsequences are [1, 3, 4, 7] and [1, 3, 5, 7].
Input: nums = [2,2,2,2,2]
Output: 5
Explanation: The length of the longest increasing subsequence is 1, and there are 5 increasing subsequences of length 1, so output 5.
The problem is to find the number of longest increasing subsequences in the given array. Here, we will use a dynamic programming approach where one array will keep track of the length of the longest increasing subsequence ending at each index, and another array will keep the number of such subsequences.
lengths and counts.lengths[i] will store the length of the longest increasing subsequence ending at index i.counts[i] will store the number of longest increasing subsequences ending at index i.lengths to 1 because the minimum length of an increasing subsequence ending at any element is 1 (the element itself).counts to 1 because there's at least one subsequence ending at each element (the element itself).nums[j] < nums[i].lengths and counts arrays.lengths[j] + 1 > lengths[i], it means we have found a longer subsequence by adding nums[i] to the subsequence ending at nums[j]. Thus, update lengths[i] and set counts[i] = counts[j].lengths[j] + 1 == lengths[i], it means we have found another subsequence of the same maximum length. Add counts[j] to counts[i].lengths array, which represents the length of the longest increasing subsequence.counts where corresponding lengths are equal to the maximum value found in step 5.lengths and counts to store information regarding the longest subsequences.