mediumGreedyAmazonMicrosoft
Hot Streak Finder
A sports analyst finds the contiguous game stretch with the highest cumulative point total (positive and negative scores allowed).
Given an integer array, find the contiguous subarray with the largest sum and return that sum.
Choose approach
-2
0i
1
1-3
24
3-1
42
51
6-5
74
81/9
Narrationstep 1 of 9
Init cur = best = -2.
Variables
cur=-2
best=-2
How to think about it
- Reset when cur + nums[i] < nums[i].
- O(n) single pass.
Edge cases to catch in interview
- All negative -> largest single element.
- Single element -> itself.
Examples
Example 1
1{"values":[1]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed