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:
1 | Input: |
Note:
- 1<= len(A), len(B) <= 1000
- 0 <= A[i], B[i] < 100
! subarray, not subsequence
- I. DP,
- Similar to LCS
- Time Complexity: O($L_A$x $L_B$)
- Space Complexity: O($L_A$x $L_B$)
1 | //c++ |
- II. Optimization
- DP
- Time Complexity: O($L_A$x $L_B$)
- Space Complexity: O($L_A$)
1 | //c++ |
- III. Optimization 2
- DP
- Time Complexity: O($L_A$x $L_B$)
- Space Complexity: O(1)
since dp[i][j] only depends on dp[i-1][j-1], we only need to keep it and traverse the matrix diagonally.
1 | //Java |
- IV. HashMap
1 | //c++ |
相关题目
- 115. Distinct Subsequences
count the number of distinct subsequences of S which equals T
DP, O(n) space
1 | class Solution { |
- 最大连续子序列和,乘积,最长递增子串,最长公共子串,子序列等问题(动态规划等)
- Some programs are online.