Maximum subarray sum
Implement `max_subarray(nums)`: the maximum sum of a non-empty contiguous
subarray.
Rules:
- an empty list returns `0`;
- if every number is negative, the answer is the largest (least negative) number,
not `0`;
- overflow is not a concern: Python integers are unbounded.
Example: `[-2, 1, -3, 4, -1, 2, 1, -5, 4]` → `6`, the subarray `[4, -1, 2, 1]`.
The expected solution is one pass (Kadane's algorithm): at each step decide
whether to extend the current run or start a new one at the current element.
The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.
Log in and askHint 1
What do you need to know about the best run that ends at the current element, so
that you do not enumerate every subarray?