LeetCode solutions

1105. Uncrossed Lines

My accepted Python solution to LeetCode problem 1105, Uncrossed Lines, running in 81ms.

  • Difficulty: Medium
  • Python
  • Runtime 81ms
  • Memory 17.8MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 81ms, memory 17.8MB, accepted 2026-01-02.

python
class Solution:
    def maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int:
        # This is equivalent to Longest Common Subsequence
        m, n = len(nums1), len(nums2)
        dp = [[0] * (n + 1) for _ in range(m + 1)]
        
        for i in range(1, m + 1):
            for j in range(1, n + 1):
                if nums1[i-1] == nums2[j-1]:
                    dp[i][j] = dp[i-1][j-1] + 1
                else:
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1])
        
        return dp[m][n]

Source