Skip to content

Commit f1bdb0e

Browse files
[N-0] add 718
1 parent 1165b49 commit f1bdb0e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Your ideas/fixes/algorithms are more than welcome!
2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
2525
|719|[Find K-th Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_719.java) | O(nlogw + nlogn) | O(1) | Hard | Binary Search
26+
|718|[Maximum Length of Repeated Subarray](https://leetcode.com/problems/maximum-length-of-repeated-subarray/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_718.java) | O(m*n) | O(m*n) | Medium | DP
2627
|717|[1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_717.java) | O(n) | O(1) | Easy |
2728
|714|[Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_714.java) | O(n) | O(1) | Medium | DP
2829
|713|[Subarray Product Less Than K](https://leetcode.com/problems/subarray-product-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_713.java) | O(n) | O(1) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 718. Maximum Length of Repeated Subarray
5+
*
6+
* Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.
7+
8+
Example 1:
9+
Input:
10+
A: [1,2,3,2,1]
11+
B: [3,2,1,4,7]
12+
Output: 3
13+
14+
Explanation:
15+
The repeated subarray with maximum length is [3, 2, 1].
16+
Note:
17+
1 <= len(A), len(B) <= 1000
18+
0 <= A[i], B[i] < 100
19+
*/
20+
public class _718 {
21+
public static class Solution1 {
22+
public int findLength(int[] A, int[] B) {
23+
if (A == null || B == null || A.length == 0 || B.length == 0) {
24+
return 0;
25+
}
26+
int[][] dp = new int[A.length + 1][B.length + 1];
27+
int result = 0;
28+
for (int i = A.length - 1; i >= 0; i--) {
29+
for (int j = B.length - 1; j >= 0; j--) {
30+
if (A[i] == B[j]) {
31+
dp[i][j] = dp[i + 1][j + 1] + 1;
32+
}
33+
result = Math.max(result, dp[i][j]);
34+
}
35+
}
36+
return result;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)