maximum-subarray

This commit is contained in:
Gleb Koval 2021-11-05 14:32:37 +00:00
parent d31b918c96
commit b81e5b836a
No known key found for this signature in database
GPG Key ID: 7C89CDC822F8392B
1 changed files with 17 additions and 0 deletions

17
maximum-subarray/sol.go Normal file
View File

@ -0,0 +1,17 @@
package main
// Time: O(n)
// Space: O(1)
func maxSubArray(nums []int) int {
current, max := 0, nums[0]
for _, num := range nums {
current += num
if current > max {
max = current
}
if current < 0 {
current = 0
}
}
return max
}