Maximum Continuous Sum
Consider this list:
There are many contiguous sums:
What’s the maximum contiguous sum we could get? In this case,
Use: if the values are, for example, stock changes (increase/decrease) then we’re asking for the maximum contiguous increase
Brute Force
Check all possible contiguous sums and take the max Pseudocode
max = A[0]
for i = 0 to n-1
sum = 0
for j = i to n-1
sum = sum + A[j] # time C
if sum > max # |
max = sum # V
end
end
endTime complexity is then
Divide & Conquer
Basic D&C methodology is to split the list into sublists (usually 2) and ask the same question about those sublists and combine the answers. For the sublists, do the same thing via recursion. Base case is generally a list of length 1. In this case, MCS(list of length 1) = that element. Idea: Given a list, split it
In addition, we’ll manually find the straddling max - the max which includes at least one element from each side of the split.