1035 uncrossed lines

·data-structure-and-algorithm
#dynamic-programming

1035. 不相交的线

go:

func maxUncrossedLines(nums1 []int, nums2 []int) int {
    n, m := len(nums1), len(nums2)
    dp := make([]int, m + 1)

    for i := 1; i <= n; i++ {
        pre := 0

        for j := 1; j <= m; j++ {
            temp := dp[j]

            if nums1[i - 1] == nums2[j - 1] {
                dp[j] = pre + 1
            } else if dp[j - 1] > dp[j] {
                dp[j] = dp[j - 1]
            }

            pre = temp
        }
    }

    return dp[m]
}