-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumLengthOfRepeatedSubarray.swift
More file actions
42 lines (33 loc) · 1.03 KB
/
maximumLengthOfRepeatedSubarray.swift
File metadata and controls
42 lines (33 loc) · 1.03 KB
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
/*
718. Maximum Length of Repeated Subarray
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.
Example 1:
Input:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
Output: 3
Explanation:
The repeated subarray with maximum length is [3, 2, 1].
Note:
1. 1 <= len(A), len(B) <= 1000
0. 0 <= A[i], B[i] < 100
https://leetcode.com/problems/maximum-length-of-repeated-subarray/
*/
class Solution {
func findLength(_ A: [Int], _ B: [Int]) -> Int {
guard !A.isEmpty, !B.isEmpty else { return 0 }
// dp[i][j] is the longest common prefix of A[i:], B[j:]
var dp = [[Int]](repeating: [Int](repeating: 0, count: B.count+1),
count: A.count+1)
var result = 0
for i in A.indices.reversed() {
for j in B.indices.reversed() {
if A[i] == B[j] {
dp[i][j] = dp[i+1][j+1] + 1
result = max(result, dp[i][j])
}
}
}
return result
}
}