This problem is a modified version of Longest Increasing Subsequence.
For this problem, we still need to find out the LIS, which is quite simple:
for j in range(i):
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j]+1)
Note that dp[i]
represents for the LIS for element i
. if dp[i] == dp[j] + 1 and nums[i] > nums[j]
, this means that element i
can be accessed from j
:
for j in range(i):
if dp[i] == dp[j] + 1 and nums[i] > nums[j]:
count[i] += count[j]