LeetCode 53 Maximum Subarray - Solution

The core idea

Kadane's algorithm: track the maximum sum ending at each position.

At each index, decide: extend the previous subarray, or start fresh from this element.

currentSum = max(nums[i], currentSum + nums[i]).

If the running sum drops below the current element, it's better to restart. Update the global maximum as you go.