๐ 1035. ไธ็ธไบค็็บฟ
2022ๅนด6ๆ9ๆฅๅฐไบ 1 ๅ้
๐ 1035. ไธ็ธไบค็็บฟ
้พๅบฆ: ๐
้ฎ้ขๆ่ฟฐ
่งฃๆณ
class Solution {
public int maxUncrossedLines(int[] nums1, int[] nums2) {
// ๆ่ทฏ๏ผ
// dp[i][j] = dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + 1
int rows = nums1.length;
int cols = nums2.length;
int[][] dp = new int[rows + 1][cols + 1];
// ๅๅงๅ
int res = 0;
// dp
for(int i = 1; i <= rows; i ++) {
for(int j = 1; j <= cols; j ++) {
int m = i - 1;
int n = j - 1;
if(nums1[m] == nums2[n]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
res = Math.max(res, dp[i][j]);
}
}
return res;
}
}