Skip to content

Instantly share code, notes, and snippets.

@sahajre
Last active August 7, 2019 13:38
Show Gist options
  • Save sahajre/b4150a38e70e0aff1141b59dcebcab13 to your computer and use it in GitHub Desktop.
Save sahajre/b4150a38e70e0aff1141b59dcebcab13 to your computer and use it in GitHub Desktop.
package msp
func mspN1(n []int) (maxSum, mi, mj int) {
l := len(n)
if l == 0 {
return -1, -1, -1
}
maxSum = n[0]
sum := n[0]
i := 0
for j := 1; j < l; j++ {
sum += n[j]
if sum >= maxSum {
maxSum = sum
mi = i
mj = j
} else if sum < 0 {
i = j + 1
sum = 0
}
}
return maxSum, mi, mj
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment