[Leetcode] [Java] Trapping Rain Water

Source: Internet
Author: User

Test Instructions:

Given n non-negative integers representing an elevation map where the width of each bar are 1, compute how much WA ter It is the 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!

Title:

A given n non-negative integer to represent the height graph. The width of each column is 1. Calculate the amount of water that can be filled by this thing after the rain.

[0,1,0,2,1,0,1,3,2,1,2,1" 6

The height graph is represented by an array [0,1,0,2,1,0,1,3,2,1,2,1].

In this example, the rain (seen in blue) has a total of 6 units.

Algorithm Analysis:

I was really drunk when I brushed this question. This is the 15 spring of the ALI algorithm project teacher Internship Online test Questions ~ ~

Exactly, when the pen I really do not do ah, written a decisive not embarrassed ~ ~

* Observation can be found after the water is filled with the shape of the first ascending and descending tower shape. So. First go through to find the tower, and then start from both sides, the top of the tower to traverse the location, the water level will only increase not decrease,

* and has been the most recently encountered the highest level of parity, so know the real-time water level. You can calculate the area while traversing the edge.

* First to find the highest, then sweep from left to highest point.

* hit a number a[i]. Calculates whether the highest a[0,,, I-1] is higher than a[i].

* Suppose it is. The volume of water on A[i] is Max (a[0...i-1])-a[i], otherwise 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


[Leetcode][java] trapping Rain water

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.