We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 654672a commit 9a056edCopy full SHA for 9a056ed
leetcode/718.cpp
@@ -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