Maximum Subarray Sum
Problem statement
Given a nonempty integer array nums, return the largest sum of any nonempty contiguous subarray.
A subarray consists of consecutive elements. Values may be negative. Return the sum, rather than the subarray indices.
Function
maximumSubarraySum(nums: int[]) → longExamples
Example 1
nums = [-2,1,-3,4,-1,2,1,-5,4]return = 6The subarray [4, -1, 2, 1] has sum 6, the maximum.
Example 2
nums = [-8,-3,-6]return = -3The nonempty subarray [-3] is optimal; an empty subarray is not allowed.
Example 3
nums = [5]return = 5The only nonempty subarray contains 5.
Constraints
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9- The answer fits a signed 64-bit integer.