mirror of
https://github.com/EthanMarti/infio-copilot.git
synced 2026-01-16 08:21:55 +00:00
705 B
705 B
Kadane's algorithm
Kadane's algorithm is an O(n) solution to this problem.
The key idea is that for each element, we have only 2 choices:
- Add this element to the current subset.
- Start a new subset.
Due to this reasons, the problem can a viewed as a sliding window problem.
The general steps are as follows:
- Initialize two variables, say
currentSumandmaxSum. Set both of them to the first element of the array. - Loop through from the 2nd to the last element in the array.
- Decide to include the current number or to start a new subset using:
currentSum = max(num, currentSum + num) - If the current value of the subset is the best so far update
maxSum
- Decide to include the current number or to start a new subset using: