Last active
January 21, 2022 03:44
-
-
Save MorrisLaw/fd181aaacb16082cf7af58dcada3ecf3 to your computer and use it in GitHub Desktop.
time complexity is O(n) because we do at most, one iteration through the array where n is the length of the array. Space complexity is constant since we have an input array and 3 variables we are using (curr, max and i)
This file contains 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
import "math" | |
func maxSubArray(nums []int) int { | |
if nums == nil { return 0 } | |
curr := nums[0] | |
max := curr | |
for i := 1; i < len(nums); i++ { | |
curr = int(math.Max(float64(curr + nums[i]), float64(nums[i]))) | |
if curr > max { | |
max = curr | |
} | |
} | |
return max | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment