Kadane's Algorithm
Carry the best run ending here. A negative run only hurts, so drop it and start over.
Prefix Sum and Kadane: Kadane on a mixed array
The run so far totals -2, and dragging that along would only hurt. Start over at 1. That is a new best.
Cost
- Best
- O(n)
- Average
- O(n)
- Worst
- O(n)
- Space
- O(1)
Before this
Leads to
Topics that need this one first.
Taught by the same lesson
Prefix Sum and Kadane covers these too, in the same run.