dp
and dpPrev
of size n2 + 1
with all elements set to 0.nums1
using an outer loop with index i
.i
, iterate over the elements of nums2
using an inner loop with index j
.nums1[i - 1] == nums2[j - 1]
, set dp[j]
to 1 + dpPrev[j - 1]
.nums1[i - 1] != nums2[j - 1]
, set dp[j]
to the maximum of dp[j - 1]
and dpPrev[j]
.dp
to dpPrev
.dp[n2]
, which represents the maximum number of uncrossed lines.