Practice/Uber/Leetcode 53. Maximum Subarray
CodingMust
Given an integer array, find the contiguous subarray that has the largest sum and return that sum value.
A subarray is a contiguous portion of the array. For example, in the array [1, 2, 3, 4], some subarrays include [1, 2], [2, 3, 4], and [1, 2, 3, 4], but [1, 3] is not a valid subarray because the elements are not contiguous.
Example 1:
Input: nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] Output: 6 Explanation: The subarray [4, -1, 2, 1] has the largest sum of 6
Example 2:
Input: nums = [1] Output: 1 Explanation: With a single element, that element is the answer
Example 3:
Input: nums = [5, 4, -1, 7, 8] Output: 23 Explanation: The entire array [5, 4, -1, 7, 8] has the largest sum of 23
Example 4:
Input: nums = [-3, -2, -5, -1] Output: -1 Explanation: When all numbers are negative, we choose the largest value