zoukankan      html  css  js  c++  java
  • LeetCode

    Trapping Rain Water

    2014.2.10 03:25

    Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is 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 rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

    Solution:

      The first time I tried to solve this problem, I thought I was supposed to scan the array from left to right, and calculate the amount of water every "pond" can hold. Finding the boundary of every "pond" proved to be difficult, and the code proved to be wrong...(x_x)

      During the second attempt, I changed my way of thinking. Instead of caring about how much water is held in a pond, it would be easier to calculate how much water is held in one grid, it is min(max_height_left_of_here, max_height_right_of_here) - height_of_here.

      Why? Because it has to be high at both ends and low in the middle to hold any water, isn't it?

      If that calculation above yields a negative result, it's simply 0. Because here it is too high, the water flows down and cannot be held.

      In this manner, we just have to calculate how much water is held at every position h[i] and add them up.

      Two extra arrays are needed to record the maximum heights at both ends.

      Therefore, total time complexity is O(n). Space complexity is O(n) as well.

    Accepted code:

     1 // 5CE, 2WA, 1AC, "const int &x", constant value has no reference.
     2 class Solution {
     3 public:
     4     int trap(int A[], int n) {
     5         if (A == nullptr || n < 3) {
     6             return 0;
     7         }
     8         
     9         vector<int> vl(n), vr(n);
    10         int i;
    11         int max_val;
    12         int result;
    13         
    14         max_val = 0;
    15         for (i = 0; i <= n - 1; ++i) {
    16             vl[i] = max_val;
    17             max_val = mymax(max_val, A[i]);
    18         }
    19         max_val = 0;
    20         for (i = n - 1; i >= 0; --i) {
    21             vr[i] = max_val;
    22             max_val = mymax(max_val, A[i]);
    23         }
    24         
    25         result = 0;
    26         for (i = 0; i < n; ++i) {
    27             max_val = mymax(0, mymin(vl[i], vr[i]) - A[i]);
    28             result += max_val;
    29         }
    30         vl.clear();
    31         vr.clear();
    32         
    33         return result;
    34     }
    35 private:
    36     int mymin(const int x, const int y) {
    37         return (x < y ? x : y);
    38     }
    39     
    40     int mymax(const int x, const int y) {
    41         return (x > y ? x : y);
    42     }
    43 };
  • 相关阅读:
    [Automation] 自动化测试工具和测试框架大集合
    C#借助API实现黑盒自动化测试工具的编写
    C#实现RSA加密解密
    Uipath开发过程中最常见的5类错误
    tqdm介绍及常用方法
    Top-1准确率和Top-5准确率
    focal loss焦点损失
    【深度学习】卷积神经网络中Dropout、BatchNorm的位置选择
    数据增强——mixup
    数据增强之mixup算法详解
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3542216.html
Copyright © 2011-2022 走看看