Maximum Length of Repeated Subarray
Description
doc
Solutions
First Idea
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
var findLength = function (nums1, nums2) {
let max = 0
for (let i = 0; i < nums1.length; i++) {
const cur = nums1[i]
for (let j = 0; j < nums2.length; j++) {
const other = nums2[j]
if (other === cur) {
let k = 1
while (k + i < nums1.length && k + j < nums2.length) {
if (nums1[i + k] !== nums2[j + k]) {
break
}
k++
}
max = Math.max(max, k)
}
}
}
return max
}
- Time Complexity: O(n^3)
- Space Complexity: O(1)
TODO: we need a better solution for this