Trapping Rain Water

Published: May 28, 2017

“Trapping Rain Water” is one more famous algorithm problem using bars, like Skyline Problem or Lagest Rectangle in a Histogram. Compared to other two bar chart problems, this was an easier problem once I understood what I should focus on. Also, solutions are posted on many websites, which helped to solve.

However, one thing I need to care about is there’re two types of problems. One is asking a total amount, while another is asking a maximum. These are quite similar, but takes a bit different approach. This memo is a way of finding the total amount.

Problem Description

The array of integer will be given. Each element of array expresses the height of a bar at that index. Usually, the problem asks how many units of water will be trapped.

For example, when the array [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1] is given, the shape of walls look like below. Mostly, problem defines that each wall’s width is 1 for convenience. From this definition, rather than walls, it may be natural to think stacking up unit 1 blocks.


                                             ┌────┐
3  .                                         │BBBB│
                                             └────┘
                     ┌────┐                  ┌────┐┌────┐      ┌────┐
2  .                 │BBBB│                  │BBBB││BBBB│      │BBBB│
                     └────┘                  └────┘└────┘      └────┘
         ┌────┐      ┌────┐┌────┐      ┌────┐┌────┐┌────┐┌────┐┌────┐┌────┐
1  .     │BBBB│      │BBBB││BBBB│      │BBBB││BBBB││BBBB││BBBB││BBBB││BBBB│
         └────┘      └────┘└────┘      └────┘└────┘└────┘└────┘└────┘└────┘
      .     .     .     .     .     .     .     .     .     .     .     .
      0     1     2     3     4     5     6     7     8     9     10    11 

The trapped water is counted by units, which is the same size of wall blocks. If I put white boxes like water is trapped by left and right walls, it will look like below. Between 1 and 3, 1 unit of water can be trapped. Likewise, 4 units between 3 and 7, 1 unit between 8 and 10 can be trapped.



                                             ┌────┐
3  .                                         │BBBB│
                                             └────┘
                     ┌────┐┌────┐┌────┐┌────┐┌────┐┌────┐┌────┐┌────┐
2  .                 │BBBB│|    ||    ||    |│BBBB││BBBB│|    |│BBBB│
                     └────┘└────┘└────┘└────┘└────┘└────┘└────┘└────┘
         ┌────┐┌────┐┌────┐┌────┐┌────┐┌────┐┌────┐┌────┐┌────┐┌────┐┌────┐
1  .     │BBBB│|    |│BBBB││BBBB│|    |│BBBB││BBBB││BBBB││BBBB││BBBB││BBBB│
         └────┘└────┘└────┘└────┘└────┘└────┘└────┘└────┘└────┘└────┘└────┘
      .     .     .     .     .     .     .     .     .     .     .     .
      0     1     2     3     4     5     6     7     8     9     10    11 

Above example traps 6 units of water.

The idea

If I look at each index and see how much water can be saved at that index, both left and right heights are the key to find the number of blocks. Minimum of the left highest and right highest will be the water height at the specific index.

For example, at index 3, the left and right highests are 2 and 3. Minumum of 2 and 3 is 2. The height of block is 2, 2 - 2 = 0, so no water can be trapped at index 3. Another example would be index 5. The left and right highests are 2 and 3, so a water height will be 2. If the hegiht of block is subtracted, 2 - 0 = 2 is the number of blocks to stack up at index 5 as water.

Like this, I need the left and right highests at each index. To avoid repetition to find left and right highest, it’s good to save those in auxiliary arrays. For this reason, the code does a pre-processing to find highests.

Java code

Below is the Java code explained above.

The result is:

6
10

Resources

Latest Posts

Application Development by Rails Action Cable

The previous two blog posts introduced WebSocket and how to implement a WebSocket application on Ruby on Rails. This blog post digs deeper. It is a memo on creating a more realistic application by Action Cable.

Real-time App on Rails by Action Cable

The previous blog post, WebSocket on Rails by Action Cable, focused on WebSocket as a protocol. As in the previous post, by default, Rails app responds to WebSocket connection requests without any hassle. However, other than connecting and sending ping frames, it doesn’t do anything. This blog post focuses on an application side and explains how we can create a full-duplex, bidirectional app.

WebSocket on Rails by Action Cable

In the web application domain, we hear some protocol names. Absolutely, HTTP or HTTPS is the most famous protocol that all web developers know. Although there’s a mechanism of Keep-Alive, a single request/response sequence with a single client/server is all done by HTTP. The client initiates the HTTP request to the server. Once the client receives the HTTP response from the server, communication finishes. As far as HTTP is used, the server just waits and waits. Only when the request comes in, the server can send back some data to the client. This communication style is surprisingly capable of doing many things, so most web applications are satisfied with HTTP.