Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ed159d0

Browse files
committed
Add solution #718
1 parent d9b609c commit ed159d0

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 718. Maximum Length of Repeated Subarray
3+
* https://leetcode.com/problems/maximum-length-of-repeated-subarray/
4+
* Difficulty: Medium
5+
*
6+
* Given two integer arrays nums1 and nums2, return the maximum length of a subarray
7+
* that appears in both arrays.
8+
*/
9+
10+
/**
11+
* @param {number[]} nums1
12+
* @param {number[]} nums2
13+
* @return {number}
14+
*/
15+
var findLength = function(nums1, nums2) {
16+
const dp = new Array(nums1.length + 1).fill().map(() => {
17+
return new Array(nums2.length + 1).fill(0);
18+
});
19+
let result = 0;
20+
21+
for (let i = 1; i <= nums1.length; i++) {
22+
for (let j = 1; j <= nums2.length; j++) {
23+
if (nums1[i - 1] === nums2[j - 1]) {
24+
dp[i][j] = dp[i - 1][j - 1] + 1;
25+
result = Math.max(result, dp[i][j]);
26+
}
27+
}
28+
}
29+
30+
return result;
31+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@
542542
714|[Best Time to Buy and Sell Stock with Transaction Fee](./0714-best-time-to-buy-and-sell-stock-with-transaction-fee.js)|Medium|
543543
715|[Range Module](./0715-range-module.js)|Hard|
544544
717|[1-bit and 2-bit Characters](./0717-1-bit-and-2-bit-characters.js)|Easy|
545+
718|[Maximum Length of Repeated Subarray](./0718-maximum-length-of-repeated-subarray.js)|Medium|
545546
720|[Longest Word in Dictionary](./0720-longest-word-in-dictionary.js)|Medium|
546547
722|[Remove Comments](./0722-remove-comments.js)|Medium|
547548
724|[Find Pivot Index](./0724-find-pivot-index.js)|Easy|

0 commit comments

Comments
 (0)