Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

<< [30] Water trapped in an elevation map >>

You are given an array of non-negative integers that represents a two-dimensional elevation map where each element is unit-width wall and the integer is the height. Suppose it will rain and all spots between two walls get filled up. Compute how many units of water remain trapped on the map in O(N) time and O(1) space.

Examples:

>>> coding_problem_30([2, 1, 2])  # 1@1, 1 unit of water at index 1
1

>>> coding_problem_30([3, 0, 1, 3, 0, 5])  # 3@1 2@2 3@4
8

>>> coding_problem_30([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])  # 1@2 1@4 2@5 1@6 1@9
6