Trapping Rain Water Leetcode Java

Source: Internet
Author: User

Title:

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!

Exercises

Reference: Low-key small one (http://blog.csdn.net/wzy_1988/article/details/17752809) problem-solving ideas

"First, encounter such a topic do not panic, each analysis of each a[i] can trapped water capacity, and then all A[i] trapped water capacity add
Secondly, for each a[i] can trapped water capacity, depending on the height (extendable) of the left and right sides of the difference between the lower and a[i], that is volume[i] = [min (Left[i], right[i])-a[i]] * 1, here the 1 is the width, If the width of each bar is 2, multiply it by 2. "

So how do you find the right and left height of a[i]? You know, how much water can be the main look at the short board. So for each a[i], ask for a top left short plate, and then ask for a top right short plate, the two direct shortest board minus A[i] The original value is how much water can become.

So it takes two times to traverse, one from left to right, to find the highest left short plate, and one from right to left, to find the highest right short plate. The final record of the total amount of water is the end result.

The code is as follows:

1 PublicintTrapint[] A) {
2 if(A = =NULL|| A.length = = 0)
3return0;
4
5intI, max, total = 0;
6intLeft[] =Newint[A.length];
7intRight[] =Newint[A.length];
8
9 // from left to right
TenLeft[0] = a[0];
Onemax = a[0];
A for(i = 1; i < a.length; i++) {
-Left[i] = Math.max (max, a[i]);
-max = Math.max (max, a[i]);
the}
-
-// from right to left
- RIGHT[A.LENGTH-1] = a[a.length-1];
+max = a[a.length-1];
- for(i = a.length-2; I >= 0; i--) {
+Right[i] = Math.max (max, a[i]);
Amax = Math.max (max, a[i]);
at}
-
-//trapped water (when i==0, it cannot trapped any water)
- for(i = 1; i < a.length-1; i++) {
-intbit = Math.min (Left[i], right[i])-a[i];
-if(Bit > 0)
inTotal + = bit;
-}
to
+returnTotal
-}

Look at the original example of the code again:

Index:0 1 2 3 4 5 6 7 8 9 10 11

A[index]: 0 1 0 2 1 0 1 3 2 1 2 1

Left[index]: 0 1 1 2 2 2 2 3 3 3 3 3

Right[index]: 3 3 3 3 3 3 3 3 2 2 2 1

Min[i]: 0 1 1 2 2 2 2 3 2 2 2 1

Bit[i]:-0 1 0 1 2 1 0 0 1 0 0

Then the final result can be calculated according to the above table is 6.

reference:http://blog.csdn.net/wzy_1988/article/details/17752809

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.