Leetcode 42.Trapping Rain water (rain in the Groove) ideas and methods for solving problems

Source: Internet
Author: User

Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar are 1, compute how much water it is able to trap after raining.

For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.



The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of the Rain Water (blue section) is being trapped. Thanks Marcos for contributing this image!


Idea: This question is easy to see, but think carefully. More thinking more complex, feel impossible, helpless thought for a day did not fix, only can ask for information online. The final ideas such as the following: (Netizens very powerful)


(Reference website: http://www.xuebuyuan.com/1586534.html)

The last bold word of thanks Marcos means let's enlarge our imagination. Because very easy to mislead us. Assuming that there is only one case, we can use the greedy method to solve it, but. The greedy method is going to go wrong, because assuming we calculate the current lowest point, and then expand on both sides to find the highest points on both sides, then the highest point is probably the local highest point, the answer will be wrong.

There are two ways to solve a problem:

12 Side to center search

2 Search from left to right, jumping calculation

As the following analysis diagram, it is more intuitive:

Blue represents water and can see a lot of local highs are flooded.

Here the calculation of the area without the general geometry of the method, here is the two sides toward the middle, record the current second high sechight, and then use this second high point minus the current after the column. The rest is filled with water capacity.

Why the second highest? Due to the comparison of the two sides. The highest point does not move, just move the second high.

The first idea, according to the thought can write very concise program , the time complexity of O (n):


I follow the above ideas to write code such as the following:

public class Solution {public    int trap (int[] height) {        stack<integer> st = new Stack<integer> (); 
   
    if (Height.length = = 0)            return 0;        int i = 0;        int j = height.length-1;        int ans = 0;//returns the answer        int sechight = 0;//The second height (the tallest one does not move) while        (I < j) {            if (Height[i] < height[j]) {                Sechight = Math.max (Sechight,height[i]);                Since the length is 1, the height is also the area value. Assuming Height[i]==sechight, the new area is 0                ans + = sechight-height[i];                i++;            } else{                sechight = Math.max (Sechight,height[j]);                Since the length is 1, the height is also the area value. Assuming Height[i]==sechight, the new area is 0                ans + = sechight-height[j];                j--;            }        }        return ans;    }}
   



Leetcode 42.Trapping Rain water (rain in the Groove) ideas and methods for solving problems

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.