Question DSA-TP-0062
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
lm = rm = water = 0
while l < r:
if h[l] < h[r]:
lm = max(lm, h[l]); water += lm - h[l]; l += 1
else:
rm = max(rm, h[r]); water += rm - h[r]; r -= 1
print(water)
- A.
8
- B.
7
- C.
Sign up free to see the answer and a step-by-step explanation.