Find the contiguous subarray with the largest sum using Kadane's Algorithm
Define maxEndingHere[i] as the maximum subarray sum ending at index i. The recurrence is: maxEndingHere[i] = max(nums[i], maxEndingHere[i-1] + nums[i]). This optimal substructure allows computing each entry in O(1) from the previous, yielding linear total time.
A negative running sum can never improve a future subarray—resetting to zero is always optimal, making this a valid greedy algorithm.
currentSum: running sum variablemaxSum: global maximum trackerThe DP recurrence only depends on the immediately preceding value, not the entire history. This allows "space optimization" from O(n) tabulation down to O(1) by keeping only the previous state.
Kadane's algorithm is an online algorithm: it can process a stream of numbers without storing the entire input, outputting the current maximum at any point.