Data Structures & AlgorithmsHardCodeforces Div2 B · ICPC regional

Rain falls on the elevation map [4, 2, 0, 3, 2, 5] (each bar has width 1). How many units of water are trapped in total?

  1. A. 8
  2. B. 9
  3. C. 10
  4. D. 18

Correct answer: B. 9

Water above bar i is min(max height to its left, max height to its right) minus its own height. Using only the left maximum overestimates the water.

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

Exams that ask this topic