-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumLengthOfRepeatedSubarray.java
More file actions
34 lines (32 loc) · 1013 Bytes
/
MaximumLengthOfRepeatedSubarray.java
File metadata and controls
34 lines (32 loc) · 1013 Bytes
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
// https://leetcode.com/problems/maximum-length-of-repeated-subarray/
// #array #hash-table #binary-search #dynamic-programming
class Solution {
public int findLength(int[] nums1, int[] nums2) {
// dp[i][j] = do dai chuoi chung dai nhat ket thuc tai i va j
// if (nums1[i] == nums2[j])
// dp[i][j] = 1 + dp[i-1][j-1]
// else
// dp[i][j] = 0
// }
int m = nums1.length;
int n = nums2.length;
// max length of the common sub array which finish at index i of nums1 and index j of nums2
int[][] max_length = new int[m][n];
int result = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (nums1[i] == nums2[j]) {
if (i > 0 && j > 0) {
max_length[i][j] = 1 + max_length[i - 1][j - 1];
} else {
max_length[i][j] = 1;
}
result = Math.max(result, max_length[i][j]);
} else {
max_length[i][j] = 0;
}
}
}
return result;
}
}