Question DSA-TP-0015
HardWhat is the output of this code?
h = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
l, r = 0, len(h) - 1
lmax = rmax = water = 0
while l < r:
if h[l] < h[r]:
lmax = max(lmax, h[l]); water += lmax - h[l]; l += 1
else:
rmax = max(rmax, h[r]); water += rmax - h[r]; r -= 1
print(water)
- A.
6
- B.
4
- C.
Sign up free to see the answer and a step-by-step explanation.