-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path718.cpp
57 lines (56 loc) · 1.98 KB
/
718.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
47
48
49
50
51
52
53
54
55
56
57
__________________________________________________________________________________________________
sample 32 ms submission
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
unordered_map<int, vector<int>> pos;
for(int i = 0 ; i < A.size(); ++i){
pos[A[i]].push_back(i);
}
int max_array = 0;
for(int i = 0; i < B.size(); ++i){
if ( pos.find(B[i]) != pos.end() ){
int max_right_B = i;
for(int pa : pos[B[i]]){
int left_B = i;
int right_B = i;
int left_A = pa;
int right_A = pa;
while(left_B > 0 && left_A > 0 && A[left_A-1] == B[left_B-1]){
--left_A;
--left_B;
}
while(right_B < B.size()-1 && right_A < A.size()-1 && A[right_A+1] == B[right_B+1]){
++right_A;
++right_B;
}
max_right_B = max(max_right_B, right_B);
max_array = max(max_array, right_B-left_B + 1 );
}
i = max_right_B;
}
}
return max_array;
}
};
__________________________________________________________________________________________________
sample 9144 kb submission
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int res = 0, m = A.size(), n = B.size();
vector<int> dp(n + 1, 0);
for (int i = 1; i <= m; i++) {
for (int j = n; j > 0; j--) {
if (A[i - 1] == B[j - 1]) {
dp[j] = dp[j - 1] + 1;
res = max(res, dp[j]);
} else {
dp[j] = 0;
}
}
}
return res;
}
};
__________________________________________________________________________________________________