[LeetCode] [Java] Trapping Rain Water

Source: Internet
Author: User

[LeetCode] [Java] Trapping Rain Water
Question:

 

Given n non-negative integers representing an elevation map where the width of each bar is 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], Return6.

The above elevation map is represented by array [,]. in this case, 6 units of rain water (blue section) are being trapped. thanks Marcos for contributing this image!

Question:

Given n non-negative integers to represent the height chart, the width of each column is 1. Calculate the maximum amount of water that this stuff can hold after rain.

For example[0,1,0,2,1,0,1,3,2,1,2,1], Return6.

The height chart is displayed in the array. In this example, there are 6 units of rain (shown in blue.

Algorithm analysis:

I was really drunk when I caught this question. This is the online test question of Alibaba algorithm engineer internship in the spring of 15 years ~~

Exactly the same. At that time, I could not really do it. I did not make a written test ~~

* After observing, we can find that the shape of the tower filled with water is the first to rise and then fall. Therefore, first traverse the tower and find the top of the tower. Then, start from both sides and traverse to the top of the tower, the water level will only increase and will not decrease,

* The maximum height has always been the same as that recently encountered. By knowing the real-time water level, you can traverse and compute the area.

* First, find the highest value, and then scan from left to top,

* When encountering A number A [I], calculate whether the highest value of A [0, I-1] is higher than A [I],

* If yes, the volume of water on A [I] Is max (A [0... I-1])-A [I]; otherwise it is 0 and the maximum value is updated

AC code:

 

public class Solution {   public int trap(int[] height)     {        if(height==null||height.length==0)        return 0;        int res=0;        int maxvalue=0;        int label=0;        int startmvalue=0;        int endmvalue=0;        int mtem;        for(int i=0;i
 
  maxvalue)        {        maxvalue=height[i];        label=i;        }        }        startmvalue=height[0];        for(int i=0;i
  
   startmvalue) startmvalue=height[i];        else        {        res+=startmvalue-height[i];        }        }        endmvalue=height[height.length-1];        for(int i=height.length-1;i>label;i--)        {        if(height[i]>endmvalue) endmvalue=height[i];        else        {        res+=endmvalue-height[i];        }        }    return res;    }}
  
 


 

 

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.