Skip to content

Commit fa8a7bb

Browse files
committed
added problem 718
1 parent ab0c0b1 commit fa8a7bb

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

problems/problem0718/repeated_arr.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package problem0718
2+
3+
/*
4+
Given two integer arrays nums1 and nums2, return the maximum length of a subarray that appears in both arrays.
5+
*/
6+
7+
func findLength(nums1 []int, nums2 []int) int {
8+
var res int
9+
var cache [][]int = make([][]int, len(nums1)+1)
10+
for i := range cache {
11+
cache[i] = make([]int, len(nums2)+1)
12+
}
13+
for i := 1; i <= len(nums1); i++ {
14+
for j := 1; j <= len(nums2); j++ {
15+
if nums1[i-1] == nums2[j-1] {
16+
cache[i][j] = cache[i-1][j-1] + 1
17+
} else {
18+
cache[i][j] = 0
19+
}
20+
if cache[i][j] > res {
21+
res = cache[i][j]
22+
}
23+
}
24+
}
25+
return res
26+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package problem0718
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input, Second []int
12+
Expected int
13+
}
14+
15+
var Results = []Result{
16+
{[]int{1, 2, 3, 2, 1}, []int{3, 2, 1, 4, 7}, 3},
17+
{[]int{0, 0, 0, 0, 0}, []int{0, 0, 0, 0, 0}, 5},
18+
}
19+
20+
func TestRepeatedSubarrayLen(t *testing.T) {
21+
assert := assert.New(t)
22+
23+
for _, res := range Results {
24+
want := res.Expected
25+
got := findLength(res.Input, res.Second)
26+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
27+
}
28+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Each problem is in it's own directory, with test files. There are helper package
121121
| 0665 | [Non-decreasing Array](https://leetcode.com/problems/non-decreasing-array) | [My Solution](./problems/problem0665) ||
122122
| 0695 | [Max Area of Island](https://leetcode.com/problems/max-area-of-island) | [My Solution](./problems/problem0695) ||
123123
| 0704 | [Binary Search](https://leetcode.com/problems/binary-search) | [My Solution](./problems/problem0704) ||
124+
| 0718 | [Maximum Length of Repeated Subarray](https://leetcode.com/problems/maximum-length-of-repeated-subarray) | [My Solution](./problems/problem0718) ||
124125
| 0729 | [My Calendar I](https://leetcode.com/problems/my-calendar-i) | [My Solution](./problems/problem0729) ||
125126
| 0733 | [Flood Fill](https://leetcode.com/problems/flood-fill) | [My Solution](./problems/problem0733) ||
126127
| 0743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time) | [My Solution](./problems/problem0743) ||

0 commit comments

Comments
 (0)