class Solution {
public int maxUncrossedLines(int[] nums1, int[] nums2) {
int[][] dp = new int[nums1.length + 1][nums2.length + 1];
int max = 0;
for(int i = 1; i < nums1.length + 1;i++){
for(int j = 1; j < nums2.length + 1;j++){
if(nums1[i - 1] == nums2[j - 1]){
dp[i][j] = dp[i - 1][j - 1] + 1;
} else{
dp[i][j] = Math.max(dp[i - 1][j],dp[i][j - 1]);
}
}
}
return dp[nums1.length][nums2.length];
}
}
其实就是最大公共子序列
最大公共子序列:不相交线段的动态规划解法
本文探讨了一种使用动态规划解决不相交线段问题的方法,通过实例展示了Solution类中的maxUncrossedLines函数,该函数计算两个整数数组中可以形成最长公共子序列的线段数量。这是一种将最大公共子序列问题应用到实际线段比较中的实例。
425

被折叠的 条评论
为什么被折叠?



