MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / maxSubArray

Function maxSubArray

maximum_subarray_53/solution.go:9–19  ·  view source on GitHub ↗

Note: study again Kadane's Algorithm works for this problem https://www.youtube.com/watch?v=86CQq3pKSUw Remembering past sums (dynamic programming) reduces runtime.

(nums []int)

Source from the content-addressed store, hash-verified

7// https://www.youtube.com/watch?v=86CQq3pKSUw
8// Remembering past sums (dynamic programming) reduces runtime.
9func maxSubArray(nums []int) int {
10 maxSum := nums[0]
11 maxCurrent := nums[0]
12
13 for i := 1; i < len(nums); i++ {
14 maxCurrent = int(math.Max(float64(nums[i]), float64(maxCurrent+nums[i])))
15 maxSum = int(math.Max(float64(maxSum), float64(maxCurrent)))
16 }
17
18 return maxSum
19}
20
21// Solved for the 2nd time
22func maxSubArray2(nums []int) int {

Callers 1

Test_maxSubArrayFunction · 0.85

Calls

no outgoing calls

Tested by 1

Test_maxSubArrayFunction · 0.68