Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 673 Number of Longest Increasing Subsequences - Dual State

Length and count

You need two arrays: length[i]length[i] = length of LIS ending at ii count[i]count[i] = number of LIS of that length ending at ii When extending from jj to ii (where arr[j]<arr[i]arr[j] < arr[i]):

1.1. If length[j]+1>length[i]length[j] + 1 > length[i]: found a longer path. Set length[i]=length[j]+1length[i] = length[j] + 1 and count[i]=count[j]count[i] = count[j].

2.2. If length[j]+1=length[i]length[j] + 1 = length[i]: found another path of same length. Add count[j]count[j] to count[i]count[i]. Base case: length[i]=1length[i] = 1, count[i]=1count[i] = 1 for all ii.