[Leetcode 42] Trapping Rain Water

Source: Internet
Author: User

Title Link: trapping-rain-water


Import java.util.stack;/** * Given n non-negative integers representing an elevation map where the width of each bar is 1, Compute how much water it was 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 are represented by array [0,1,0,2,1,0,1,3,2, 1,2,1]. 6 units of rain Water (blue section) is being trapped. Thanks Marcos for contributing this image! * */public class Trappingrainwater {//Solution one//315/315 test Cases passed.//status:accepted//runtime:248 ms//submitted:0 minutes ago//Time complexity O (n), Space complexity O (1) public int trap (int[] A) {int sum = 0;//int ma          x = 0;     for (int i = 0; i < a.length; i++) {//Find maximum if (A[i] > A[max]) {max = i;}}        Calculates the left int topest = 0;         for (int i = 0; i < max; i++) {if (A[i] > topest) {topest = A[i];} else {sum + = Topest-a[i];}}        Calculate Right topest = 0; for (int i = a.length-1; i > max; i--) {if (A[i] > Topest) {topest = A[i];} else {sum + = Topest-a[i];}}        return sum;        }//Solution two//315/315 test Cases passed.//status:accepted//runtime:282 ms//submitted:1 minute ago Time complexity O (n) space complexity O (n) public int trap1 (int[] A) {stack<integer> Stack = new stack<integer> (); int sum = 0;int i = 0;int max = maximum value in the 0;//stack for (; i < a.length-1; i++) {//Find leftmost maximum if (A[i] > a[i + 1]) {Stack.add (a[i]); MA x = A[i];break;}} for (i = i + 1; i < a.length; i++) {int count = 1;while (!stack.isempty () && stack.peek () <= a[i]) {//if the top element of the stack Less than the current element if (A[i] >= max) {//If the current element is greater than or equal to the maximum value in the stack while (!stack.isempty ()) {//will stack all the elements out of the stack sum + = Max-stack.pop ();} max = a[i];//Sets the maximum value of the current stack} else {//If the current element is less than the maximum value in the stack = + a[i]-stack.pop () count++;//cumulative A[i] The number of times to go into the stack}}while (count! = 0) {stack. Add (A[i]); count--;}} return sum;} public static void Main (string[] args) {}}


[Leetcode 42] Trapping Rain Water

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.