Maximum Subarray
ID: 35; easy
Solution 1 (Go)
func maxSubArray(nums []int) int {
if len(nums) == 1 {
return nums[0]
}
max, result := nums[0], 0
for i := 0; i < len(nums); i++ {
result += nums[i]
// only update result when it can be increased
if result > max {
max = result
}
if result < 0 {
result = 0
}
}
return max
}Solution 2 (Go)
Solution 3 (Java)
Last updated
Was this helpful?