Problem · Array
Maximum Subarray
Learn this problemProblem statement
Given a non-empty integer array nums, return the maximum possible sum of a non-empty contiguous subarray.
The result is a signed 64-bit integer.
Function
maxSubarraySum(nums: int[]) → longExamples
Example 1
nums = [-2,1,-3,4,-1,2,1,-5,4]return = 6The contiguous subarray [4, -1, 2, 1] has the maximum sum, 6.
Example 2
nums = [-8,-3,-6]return = -3A valid subarray must be non-empty, so the best choice is the single value -3.
Constraints
1 <= nums.length <= 200000-10^9 <= nums[i] <= 10^9