Dynamic
Programming Class-work
Recall the Maximum Sub-array Sum problem we solved using Divide and Conquer
in O(nlgn). Can we solve it using Dynamic Programming?
Consider the sub-structure:
mss[i] := the maximal sum of a sub-array of A ending at index i.
(i) What is the base case, mss[1] = ?
i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
A 13 -3 -25 20 -3 -16 -23 18 20 -7 12 -5 -22 15 -4 7
mss
prev[i]
(ii) What is the recurrence for mss[i]?
(iii) What do you store is prev[i]?
(iv) Write the code to compute mss[i] and prev[i]. Add code that uses mss[i] and
prev[i] to return the maximum subarray sum and the starting and ending
indices of the maximum sum array.