forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
46 lines (35 loc) · 1.07 KB
/
main2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// Source : https://leetcode.com/problems/maximum-length-of-repeated-subarray/description/
/// Author : liuyubobobo
/// Time : 2017-10-19
#include <iostream>
#include <vector>
using namespace std;
/// Dynamic Programming
///
/// Time Complexity: O(len(A)*len(B))
/// Space Complexity: O(len(A)*len(B))
class Solution {
private:
int dp[1001][1001];
public:
int findLength(vector<int>& A, vector<int>& B) {
for(int j = 0 ; j <= B.size() ; j ++)
dp[A.size()][j] = 0;
int best = 0;
for(int i = A.size() - 1 ; i >= 0 ; i --)
for(int j = B.size() - 1 ; j >= 0 ; j --)
if(A[i] == B[j]){
dp[i][j] = 1 + dp[i+1][j+1];
best = max(best, dp[i][j]);
}
return best;
}
};
int main() {
int numsA[] = {1, 2, 3, 2, 1};
vector<int> vecA(numsA, numsA + sizeof(numsA)/sizeof(int));
int numsB[] = {3, 2, 1, 4, 7};
vector<int> vecB(numsB, numsB + sizeof(numsB)/sizeof(int));
cout << Solution().findLength(vecA, vecB) << endl;
return 0;
}