53. Maximum Subarray

gegeben: gesucht: falls alle

anders formuliert

The Naive Approach

for for

Anzahl Addition:

Reduce Duplicate Work

Using Prefix sum

Divide and Conquer

divide and conquer.excalidraw

⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’

Excalidraw Data

Text Elements

n/2

n/2

Fall 1

Fall 2

Fall 3

Lösung ganz links

Lösung ganz rechts

Lösung geht über die Mitte

(A(n) ≤ n/2-1)

Link to original

: falls sonst

Another approach

Master Theorem

NOTE

Example

Link to original

Dynamic Programming

class Solution {
public:
 int maxSubArray(vector<int> &nums) {
 int n = nums.size();
 int currentsum = 0;
 int maxs = INT_MIN;
 int maxsum = INT_MIN;
 for (int i = 0; i < n; i++) {
 currentsum += nums[i];
 maxs = max(maxs, currentsum);
 if (currentsum < 0) {
 maxsum = max(maxsum, maxs);
 currentsum = 0;
 maxs = INT_MIN;
 }
 }
 maxsum = max(maxsum, maxs);
 return maxsum;
 }
};