Created
January 2, 2023 11:06
-
-
Save frectonz/5e74607c83c0e1f0a6ef793e60411dac to your computer and use it in GitHub Desktop.
From cassido's January 2, 2023 Newsletter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
res1 := maxSubArray([]int{-4, 2, -5, 1, 2, 3, 6, -5, 1}, 4) | |
fmt.Println(res1) | |
res2 := maxSubArray([]int{1, 2, 0, 5}, 2) | |
fmt.Println(res2) | |
} | |
func maxSubArray(list []int, size int) []int { | |
var maxSum int | |
var maxSubArray []int | |
for i := 0; i <= len(list)-size; i++ { | |
currentSubArray := list[i : i+size] | |
var sum int | |
for _, n := range currentSubArray { | |
sum += n | |
} | |
if sum > maxSum { | |
maxSum = sum | |
maxSubArray = currentSubArray | |
} | |
} | |
return maxSubArray | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment