Maximum Subarray
Given an integer array nums, find the contiguous, non-empty subarray (a run of one or more neighbouring elements) with the largest sum, and return that sum.
The subarray must contain at least one element, even when every number is negative.
Constraints
1 <= nums.length <= 10^5-10^4 <= nums[i] <= 10^4- Aim for O(n) time: checking every subarray is O(n²).
Examples
Example 1
Input: nums = [3,-4,5,-1,2,-6,4] Output: 6
[5, -1, 2] sums to 6. Adding the 3 and -4 in front would contribute -1, which only lowers it.
Example 2
Input: nums = [-3,-1,-2] Output: -1
Every number is negative and the subarray can't be empty, so the best is the single largest number.
Example 3
Input: nums = [7] Output: 7
A single element is its own only subarray.
JavaScript you'll use: Numbers, Math and integer tricks Arrays: create, read, update
/**
* @param {number[]} nums
* @return {number}
*/
function maxSubArray(nums) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.