vendredi 29 mars 2019

Finding max sum contiguous subarray when atmost k elements can be omitted?

Eg- n=10
arr[]={6,-5,3,-7,6,-1,10,-8,-8, 8}
For k=0, best segment is 5-7 with sum=15. For k=2, best segment is 1-7 with sum=24.
For k=6, best segment is 1-10 with sum=33.

I think, let dp[i][j] denote the maximum segment ending at position i with at most j elements dropped. Inductively compute dp[i][j]from dp[i][j-1]. But how to keep track of elements removed from dp[i][j-1] and not to remove them again??? I am confused in dp problems. I want to know dp approach and some other approach if possible?

Aucun commentaire:

Enregistrer un commentaire