Skip to content

Commit 97c5550

Browse files
committed
added problem 523
1 parent fec67ee commit 97c5550

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

problems/problem0523/subarr_sum.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package problem0523
2+
3+
/*
4+
Given an integer array nums and an integer k,
5+
return true if nums has a continuous subarray of size at least two whose elements sum up to a multiple of k, or false otherwise.
6+
An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.
7+
*/
8+
9+
func checkSubarraySum(nums []int, k int) bool {
10+
var sum int
11+
for i := 0; i < len(nums)-1; i++ {
12+
sum = nums[i]
13+
for j := i + 1; j < len(nums); j++ {
14+
sum += nums[j]
15+
if sum%k == 0 {
16+
return true
17+
}
18+
}
19+
}
20+
return false
21+
}
22+
23+
func checkSubarraySumOpt(nums []int, k int) bool {
24+
var seen = map[int]int{0: -1}
25+
var sum int
26+
for i := range nums {
27+
sum += nums[i]
28+
sum %= k
29+
if prev, ok := seen[sum]; ok {
30+
if i-prev > 1 {
31+
return true
32+
}
33+
} else {
34+
seen[sum] = i
35+
}
36+
}
37+
return false
38+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package problem0523
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input []int
12+
Target int
13+
Expected bool
14+
}
15+
16+
var Results = []Result{
17+
{[]int{23, 2, 4, 6, 7}, 6, true},
18+
{[]int{23, 2, 6, 4, 7}, 6, true},
19+
{[]int{23, 2, 6, 4, 7}, 13, false},
20+
{[]int{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, 1751423, false},
21+
}
22+
23+
func TestSubarraySumEqual(t *testing.T) {
24+
assert := assert.New(t)
25+
26+
for _, res := range Results {
27+
want := res.Expected
28+
got := checkSubarraySum(res.Input, res.Target)
29+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
30+
}
31+
}
32+
33+
func BenchmarkSubarraySumEqual(b *testing.B) {
34+
for i := 0; i < b.N; i++ {
35+
for _, res := range Results {
36+
checkSubarraySum(res.Input, res.Target)
37+
}
38+
}
39+
}
40+
41+
func TestSubarraySumEqualOpt(t *testing.T) {
42+
assert := assert.New(t)
43+
44+
for _, res := range Results {
45+
want := res.Expected
46+
got := checkSubarraySumOpt(res.Input, res.Target)
47+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
48+
}
49+
}
50+
51+
func BenchmarkSubarraySumEqualOpt(b *testing.B) {
52+
for i := 0; i < b.N; i++ {
53+
for _, res := range Results {
54+
checkSubarraySumOpt(res.Input, res.Target)
55+
}
56+
}
57+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ Each problem is in it's own directory, with test files. There are helper package
124124
| 0501 | [Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree) | [My Solution](./problems/problem0501) ||
125125
| 0509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number) | [My Solution](./problems/problem0509) ||
126126
| 0515 | [Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row) | [My Solution](./problems/problem0515) ||
127+
| 0523 | [Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum) | [My Solution](./problems/problem0523) ||
127128
| 0539 | [Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference) | [My Solution](./problems/problem0539) ||
128129
| 0542 | [01 Matrix](https://leetcode.com/problems/01-matrix) | [My Solution](./problems/problem0542) ||
129130
| 0543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree) | [My Solution](./problems/problem0543) ||

0 commit comments

Comments
 (0)