Data Structures & AlgorithmsHard

Why does 'maximum number of disjoint zero-sum blocks' use an interval DP over prefixes rather than a greedy scan?

  1. A. Greedy always gives the optimal answer for this problem.
  2. B. Zero-sum blocks cannot overlap with each other by definition.
  3. C. The DP is needed only when the array is sorted.
  4. D. Greedily taking the first zero-sum block can block a better packing of shorter blocks, so the optimal count needs the best value over all possible ends.

Correct answer: D. Greedily taking the first zero-sum block can block a better packing of shorter blocks, so the optimal count needs the best value over all possible ends.

A greedy choice of the earliest block can consume elements that would form two smaller zero-sum blocks, so the DP compares, for every end position, all possible last blocks. This keeps the optimum exact.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic