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 simple to see, but think carefully, the more you want more complex, feel impossible, helpless think of a day did not fix, can only help online information, finally thinking as follows: (Netizens very strong)


(reference URL: http://www.xuebuyuan.com/1586534.html)

The last bold word of thanks Marcos means to enlarge our imagination because it is easy to mislead us. If only the case, we can use greedy method to solve, but the greedy method will be wrong, because if 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 likely to be 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 many of the local highs can be seen flooding.

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 of the water capacity is installed.

Why the second highest? Because the two sides compare, the highest point does not move, only moves 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 the code as follows:

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]);                Because the length is 1, the height is the area value, if height[i]==sechight, then the new area is 0                ans + = sechight-height[i];                i++;            } else{                sechight = Math.max (Sechight,height[j]);                Because the length is 1, the height is the area value, if height[i]==sechight, then the new area is 0                ans + = sechight-height[j];                j--;            }        }        return ans;    }}
   



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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

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.