## Clarifying Questions
Assuming the input is a list of integers with standard constraints (like the length can be up to 2000), we can proceed.
lengths
and counts
:
lengths[i]
will store the length of the longest increasing subsequence that ends with the element at index i
.counts[i]
will store the number of LIS that end with the element at index i
.1
s because each element by itself is an increasing subsequence of length 1.i
, check the elements before it (from index 0
to i-1
).nums[j] < nums[i]
, it means that nums[i]
can be appended to the increasing subsequence ending at j
.
lengths
and counts
arrays accordingly.lengths
array.counts
array where the corresponding value in lengths
array is equal to the maximum length.def findNumberOfLIS(nums):
if not nums:
return 0
n = len(nums)
lengths = [1] * n
counts = [1] * n
for i in range(n):
for j in range(i):
if nums[j] < nums[i]:
if lengths[j] + 1 > lengths[i]:
lengths[i] = lengths[j] + 1
counts[i] = counts[j]
elif lengths[j] + 1 == lengths[i]:
counts[i] += counts[j]
max_length = max(lengths)
return sum(c for l, c in zip(lengths, counts) if l == max_length)
# Example usage:
nums = [1, 3, 5, 4, 7]
print(findNumberOfLIS(nums)) # Output: 2
lengths
and counts
arrays.This solution efficiently calculates and counts the longest increasing subsequences in a given list.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?