1035.cpp (605B)
1 class Solution { 2 int dp[500][500]; 3 4 public: 5 Solution() { memset(dp, 0xFF, sizeof(dp)); } 6 7 int maxUncrossedLines(const vector<int> &nums1, const vector<int> &nums2, int i = 0, int j = 0) { 8 if (i >= nums1.size() || j >= nums2.size()) return 0; 9 if (dp[i][j] != -1) return dp[i][j]; 10 11 int res; 12 if (nums1[i] == nums2[j]) 13 res = 1 + maxUncrossedLines(nums1, nums2, i + 1, j + 1); 14 else { 15 res = max(maxUncrossedLines(nums1, nums2, i + 1, j), maxUncrossedLines(nums1, nums2, i, j + 1)); 16 } 17 18 return dp[i][j] = res; 19 } 20 };