[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:

Given n non-negative integers to represent the height graph, each column has a width of 1. Calculate the amount of water that can be filled by the east after the rain.

For example, given [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 (shown in blue) is 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 engineer 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 rise and then drop the tower shape, therefore, first traverse to find the tower, and then start from both sides, to the top of the tower to traverse the position, the water level will only increase will not decrease,

* And always with the most recently encountered the highest level, so know the real-time water level, you can traverse the edge to calculate the area.

* First to find the highest, then sweep from left to highest place,

* hit a number a[i], calculate a[0,,, I-1] highest is higher than a[i],

* If yes, 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


Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[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.