Leetcode - 53. Maximum Subarray
Leetcode - 53. Maximum Subarray
- Given an integer array nums, find the subarray with the largest sum, and return its sum.
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func maxSubArray(nums []int) int {
sumTotal := -10000
nextPossibleSum := 0
for i:=0; i<len(nums); i++{
nextPossibleSum = nextPossibleSum+nums[i]
if nextPossibleSum>sumTotal{
sumTotal = nextPossibleSum
}
if nextPossibleSum<0{
nextPossibleSum = 0
}
}
return sumTotal
}
This post is licensed under CC BY 4.0 by the author.