Skip to content

Commit c412c60

Browse files
committed
feat(solutions): add maximum_subarray
1 parent 3cd46fe commit c412c60

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
| # | Problem | Solution | Difficulty | Single Repetition Duration | LeetCode Run Time |
2626
| ---: | :----- | :--------: | :----------: | ----------: | ----------: |
27+
|53|[Maximum Subarray][Solutions-53]|[WindomZ][Solutions-53-Go]|Easy|[10.3 ns/op/6 test cases][Solutions-53-Test]|12 ms|
2728
|50|[Pow(x, n)][Solutions-50]|[WindomZ][Solutions-50-Go]|Medium|[7.31 ns/op/12 test cases][Solutions-50-Test]|3 ms|
2829
|49|[Group Anagrams][Solutions-49]|[WindomZ][Solutions-49-Go]|Medium|[313 ns/op/3 test cases][Solutions-49-Test]|582 ms|
2930
|48|[Rotate Image][Solutions-48]|[WindomZ][Solutions-48-Go]|Medium|[12.1 ns/op/3 test cases][Solutions-48-Test]|3 ms|
@@ -103,6 +104,9 @@ Welcome to report bugs, suggest ideas and discuss on [issues page](https://githu
103104
### Support
104105
If you like it then you can put a :star:Star on it.
105106

107+
[Solutions-53-Test]:solutions/maximum_subarray/maxSubArray_test.go#L18
108+
[Solutions-53-Go]:solutions/maximum_subarray/maxSubArray.go
109+
[Solutions-53]:https://leetcode.com/maximum-subarray/
106110
[Solutions-50-Test]:solutions/powx_n/myPow_test.go#L26
107111
[Solutions-50-Go]:solutions/powx_n/myPow.go
108112
[Solutions-50]:https://leetcode.com/powx-n/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package maximumsubarray
2+
3+
func maxSubArray(nums []int) int {
4+
if len(nums) == 0 {
5+
return 0
6+
}
7+
max, sum := nums[0], nums[0]
8+
for i := 1; i < len(nums); i++ {
9+
// get maximum value between the sum of the contiguous subarray and the current number
10+
sum = maxInt(sum+nums[i], nums[i])
11+
// get the largest sum
12+
max = maxInt(max, sum)
13+
}
14+
return max
15+
}
16+
17+
func maxInt(x, y int) int {
18+
if x >= y {
19+
return x
20+
}
21+
return y
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package maximumsubarray
2+
3+
import (
4+
"testing"
5+
6+
"github.com/WindomZ/testify/assert"
7+
)
8+
9+
func Test_maxSubArray(t *testing.T) {
10+
assert.Equal(t, 0, maxSubArray([]int{}))
11+
assert.Equal(t, 7, maxSubArray([]int{1, 2, 3, -4, 5}))
12+
assert.Equal(t, 6, maxSubArray([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4}))
13+
assert.Equal(t, 14, maxSubArray([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4, 9, -6}))
14+
assert.Equal(t, 18, maxSubArray([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4, 9, -6, 10, -12}))
15+
assert.Equal(t, 10, maxSubArray([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4, -9, -6, 10, -12}))
16+
}
17+
18+
func Benchmark_maxSubArray(b *testing.B) {
19+
b.StopTimer()
20+
b.ReportAllocs()
21+
b.StartTimer()
22+
b.RunParallel(func(pb *testing.PB) {
23+
for pb.Next() {
24+
maxSubArray([]int{})
25+
maxSubArray([]int{1, 2, 3, -4, 5})
26+
maxSubArray([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4})
27+
maxSubArray([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4, 9, -6})
28+
maxSubArray([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4, 9, -6, 10, -12})
29+
maxSubArray([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4, -9, -6, 10, -12})
30+
}
31+
})
32+
}

0 commit comments

Comments
 (0)