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.

思路:

我自己的想法,先遍历一遍数组找到所有的山峰位置,在例子中是1,3,7,10,然后再填充两个山峰之间的水,获得水量

class Solution {
public:
int trap(int A[], int n) {
bool haswater = false;
vector<int> peak;
int ans = ;
for(int i = ; i < n - ; i++) //找到第一个山峰
{
if(A[i] > A[i + ])
{
peak.push_back(i);
break;
}
}
if(peak.empty())
{
return ; //没有山谷 没水
}
int maxheight = A[peak[]];
for(int i = peak[] + ; i < n; i++) //找剩下的山峰
{
if(A[i] > A[i - ]) //比上一个值大 新山峰
{
while(A[i] > A[peak.back()] && A[peak.back()] < maxheight) //比之前的山峰高,且之前的山峰不是最高峰,删掉之前的 第一个山峰不能删
{
peak.pop_back();
}
maxheight = (A[i] > maxheight) ? A[i] : maxheight; //新的最高峰
peak.push_back(i);
}
} for(int i = ; i < peak.size() - ; i++) //获得水量 依次向两个山峰间加水
{
int height = min(A[peak[i]], A[peak[i+]]); //水平面是两个山峰中矮一点的那个
for(int j = peak[i] + ; j < peak[i+]; j++)
{
ans += max(height - A[j], ); //防止5 4 1 2 这种水平面比中间非山峰处低的情况
}
} return ans;
}
};

大神的思路:没有像我一样先找所有的山峰,而是边遍历边获得每个坐标位置的水量。 而且大神是左右两边收缩的。

class Solution {
public:
int trap(int A[], int n) {
int left=; int right=n-;
int res=;
int maxleft=, maxright=;
while(left<=right){ //没有遍历结束
if(A[left]<=A[right]){ //如果右边比较高 则处理左边
if(A[left]>=maxleft) maxleft=A[left]; //如果当前的值比左边最大值还大,那更新最大值 这个位置不会有水
else res+=maxleft-A[left]; //如果比左边最大值小那当前值一定在山谷 左边比左边最大值小 右边比右值小
left++;
}
else{
if(A[right]>=maxright) maxright= A[right];
else res+=maxright-A[right];
right--;
}
}
return res;
}
};

【leetcode】Trapping Rain Water(hard)的更多相关文章

  1. 【题解】【直方图】【Leetcode】Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  2. 【leetcode】Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

  4. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  5. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  6. [LeetCode] 407. Trapping Rain Water II 收集雨水 II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  7. [LeetCode] 42. Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. [LeetCode] 407. Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  9. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

随机推荐

  1. Aliyun OSS SDK 异步分块上传导致应用异常退出

    问题描述: 使用Aliyun OSS SDK的BeginUploadPart/EndUploadPart执行异步分块上传操作,程序出现错误并异常退出! 原因分析: Using .NET Framewo ...

  2. c# DateTime时间格式和JAVA时间戳格式相互转换

    /// java时间戳格式时间戳转为C#格式时间 public static DateTime GetTime(long timeStamp) { DateTime dtStart = TimeZon ...

  3. css3学习笔记之边框

    CSS3 圆角 border-radius 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <!DOCTYPE html> <h ...

  4. 【转载】Linux小白最佳实践:《超容易的Linux系统管理入门书》(连载六)Linux的网络配置

    本篇是Linux小白最佳实践第6篇,目的就是让白菜们了解Linux网络是如何配置的.Linux系统在服务器市场占有很大的份额,尤其在互连网时代,要使用计算机就离不开网络. 想每天能听到小妞的语音播报, ...

  5. android 数据库的增删改查

    主java package com.itheima.crud; import android.app.Activity; import android.content.Context; import ...

  6. Ubuntu 设定壁纸自动切换的shell脚本

    升级到Ubuntu14.04后,感觉bug的确比12.04少多了.顶部任务栏支持半透明效果,所以整个桌面也看上去漂亮了很多.这样的桌面也是值得瞎捣鼓一下的,想到换壁纸,但是没找到设定动态更换壁纸的选项 ...

  7. 个人站长如何使用svn发布到服务器不遗漏文件

    作为个人站长,最最头疼的一件事情就是在本地开发好代码之后,上传的时候要去服务器上一个一个文件进行覆盖,添加操作:是人难免出错,避免这种情况的方法: 开发者最好是在本地有一个代码库,创建好代码库之后,至 ...

  8. MD5加密简单算法

    public partial class MD5运用 : Form { public MD5运用() { InitializeComponent(); } private void btnChange ...

  9. Vb.Net Xml文档格式化

    最近在处理Webservice文档的时候,因为是未格式化的,需要处理,所以有了以下代码. #Region "Xml字符串转换成格式化的XML文件" 'txt_Result.Text ...

  10. Android图像处理2

    此次实验主要通过Android中的方法获取输入的颜色矩阵的值,更改后赋值给图片中的颜色矩阵更改图片效果.具体的布局的方法跟笔记1种差不多,只不过这里要添加一个供用户输入的GridView <Gr ...