Skip to content

Commit 9a056ed

Browse files
committed
add leetcode 718.cpp
1 parent 654672a commit 9a056ed

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

leetcode/718.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int findLength(vector<int>& nums1, vector<int>& nums2) {
4+
// dp[i][j] = the lcs of nums1[0..i] and nums2[0..j]
5+
// dp[i][j] = 0 if nums[i] != nums[j]
6+
// dp[i][j] = dp[i-1][j-1]+1 if nums[i] == nums[j]
7+
8+
int dp[1001][1001] = {0};
9+
int ans = 0;
10+
for (int i = 1; i <= nums1.size(); ++i) {
11+
for (int j = 1; j <= nums2.size(); ++j) {
12+
if (nums1[i-1] == nums2[j-1]) {
13+
dp[i][j] = dp[i-1][j-1] + 1;
14+
ans = max(ans, dp[i][j]);
15+
}
16+
}
17+
}
18+
19+
return ans;
20+
}
21+
};

0 commit comments

Comments
 (0)