0% found this document useful (0 votes)
58 views1 page

DP CW 2 PDF

The document discusses solving the maximum subarray sum problem using dynamic programming. It asks the reader to consider the substructure of storing the maximum subarray sum ending at each index (mss[i]) and the previous index of that maximum sum (prev[i]). It asks the reader to determine the base case for mss[i], the recurrence relation to calculate future mss[i] values, what prev[i] stores, and to write code to compute mss[i], prev[i] and return the maximum subarray sum along with its starting and ending indices.

Uploaded by

FaheemMasood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views1 page

DP CW 2 PDF

The document discusses solving the maximum subarray sum problem using dynamic programming. It asks the reader to consider the substructure of storing the maximum subarray sum ending at each index (mss[i]) and the previous index of that maximum sum (prev[i]). It asks the reader to determine the base case for mss[i], the recurrence relation to calculate future mss[i] values, what prev[i] stores, and to write code to compute mss[i], prev[i] and return the maximum subarray sum along with its starting and ending indices.

Uploaded by

FaheemMasood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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.

You might also like