zoukankan      html  css  js  c++  java
  • LeetCode42 Trapping Rain Water

    题目:

    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.  (Hard)

     分析:

    首先考虑对每个位置向左向右找到最小值,然后与本位置的值比较添加存水结果,可以做出来,时间复杂度O(n^2);

    考虑利用空间优化时间,开两个数组,分别存当前位置向左的最小值和向右的最小值。

    一次从左往右遍历更新leftHeight数组, 一次从右向左遍历更新rightHeight数组,最后一次遍历算利用上述方法算result,只不过这次可以直接从数组里读左右最小值结果。

    代码:

     1 class Solution {
     2 public:
     3     int trap(vector<int>& height) {
     4         int leftMax[height.size()];
     5         leftMax[0] = 0;
     6         for (int i = 1; i < height.size(); ++i) {
     7             leftMax[i] = max(height[i - 1], leftMax[i - 1]);
     8         }
     9         int rightMax[height.size()];
    10         rightMax[height.size() - 1] = 0;
    11         for (int i = height.size() - 2; i >= 0; --i) {
    12             rightMax[i] = max(height[i + 1], rightMax[i + 1]);
    13         }
    14         int result = 0;
    15         for (int i = 0; i < height.size(); ++i) {
    16             if (min(leftMax[i], rightMax[i]) - height[i] > 0) {
    17                 result += (min(leftMax[i], rightMax[i]) - height[i]);
    18             }
    19         }
    20         return result;
    21     }
    22 };

    上述方法将时间复杂度优化到了O(n),但利用了额外的空间,空间复杂度也提高到了O(n)。

    进一步优化,可以考虑Two pointers的思路。

    两根指针分别指向头和尾,并维护两个值,表示从左向右到p1的最大值leftHeight和从右向左到p2的最大值RightHeight。

    两个最大值中较小的向中间移动,遇到更大的值更新leftHeight或rightHeight,遇到较小的值更新result。

    这样可以做到时间复杂度O(n),空间复杂度O(1)。

    代码:

     1 class Solution {
     2 public:
     3     int trap(vector<int>& height) {
     4         if (height.size() < 2) {
     5             return 0;
     6         }
     7         int left = 0, right = height.size() - 1;
     8         int leftHeight = height[0], rightHeight = height[height.size() - 1];
     9         int result = 0;
    10         while (left < right) {
    11             if (leftHeight <= rightHeight) {
    12                 left++;
    13                 if (height[left] < leftHeight) {
    14                     result += (leftHeight - height[left]);
    15                 }
    16                 else {
    17                     leftHeight = height[left];
    18                 }
    19             }
    20             else {
    21                 right--;
    22                 if (height[right] < rightHeight) {
    23                     result += (rightHeight - height[right]);
    24                 }
    25                 else {
    26                     rightHeight = height[right];
    27                 }
    28             }
    29         }
    30         return result;
    31     }
    32 };
     
  • 相关阅读:
    Windows Mobile自动更新
    【实用代码片段】将json数据绑定到html元素 (转)
    B/S打印解决方案参考
    python基础之1-安装
    python里的生成器
    python中的函数的执行分类
    python中的return的返回与执行
    自创最精简的python装饰器
    python中的lambda函数用法
    python中的str.strip()的用法
  • 原文地址:https://www.cnblogs.com/wangxiaobao/p/5831437.html
Copyright © 2011-2022 走看看