Leetcode42:trapping Rain Water

Source: Internet
Author: User

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!


The problem had been done a long time ago, and now it came to me, and soon it was solved.

Solution One

Two-time traversal method.

A traversal is performed first, the index of the highest element is found, then a second traversal is performed, and the second traversal is divided into a traverse from the left to the maximum and a right-to-maximum value.

When traversing from the left, a variable is used to record the maximal value starting from the left, and if the currently traversed element is less than this maximum, the difference before them is the amount of water that the current point can hold, and if the current element is greater than this maximum value, the maximum value is updated. The right side of the traversal method is similar. Such as:


Runtime:8ms

Class Solution {public:    int Trap (vector<int>& height) {        int maxpos=0;        int maxheight=0;        int length=height.size ();        for (int i=0;i<length;i++)        {            if (height[i]>maxheight)            {                maxheight=height[i];                maxpos=i;            }        }        int result=0;        int leftheight=0;        for (int i=0;i<maxpos;i++)        {            if (height[i]>leftheight)            {                leftheight=height[i];            }            else            {                result+=leftheight-height[i];            }        }        int rightheight=0;        for (int i=length-1;i>maxpos;i--)        {            if (height[i]>rightheight)            {                rightheight=height[i];            }            Else            {                result+=rightheight-height[i];            }        }        return result;}    ;


Solution Two

The second solution is to use a single traversal method.

The above solution is to traverse two times, in order to find the maximum value, so as to ensure that the difference between the maximal value and the current value is the amount of water can be accommodated, but in fact can not find the maximum value but the left and right of the current value of the maximum value. If the current value on the left is large, then traverse to the right, and if the current value on the right is large, walk to the left, so that the difference between the maximum and the current value is the amount of water that can be accommodated.


Runtime:8ms

     int Trap (vector<int>& height) {        int length=height.size ();        int result=0;        int leftpos=0;        int rightpos=length-1;        int leftmax=0;        int rightmax=0;        while (Leftpos<rightpos)        {            if (Height[leftpos]


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

Leetcode42: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.