description:

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.

Note:

https://leetcode.com/problems/trapping-rain-water/.

Example:

Example:

Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

answer:

class Solution {
public:
int trap(vector<int>& height) {
stack<int> st;
int n = height.size();
int i = 0, res = 0;
while(i < n) {
if (st.empty() || height[i] <= height[st.top()]) {
st.push(i++);
}
else {
int t = st.top();
st.pop();
if (st.empty()) {
continue;
}
res += (min(height[st.top()], height[i]) - height[t]) * (i - st.top() - 1);
}
}
return res;
}
};
class Solution {
public:
int trap(vector<int>& height) {
int res = 0, n = height.size(), mx = 0;
vector<int> dp(n, 0);
for (int i = 0; i < n; ++i) {
dp[i] = mx; // 找到左边最高的
mx = max(mx, height[i]);
}
mx = 0;
for (int i = n - 1; i >= 0; --i) {
dp[i] = min(mx, dp[i]); //找到右边最高的之后取左右最高中的最小值
mx = max(height[i], mx);
if (dp[i] > height[i]) res += (dp[i] - height[i]); // 如果左右最高中的最小值比这一列的高度高,那么这一列就是可以蓄水的
}
return res;
}
};

relative point get√:

hint :

  • 第一种用堆栈去做的思路大致如下:堆栈里存入的是坐标,如果现在加入的这个数和前面那个数比小,那就一直加入,如果大,那就证明最起码能和它前面那个数形成一个水坑,但是考虑到不能重复计算水面积,所以只是覆盖他们这一层,就是这个水坑的计算是横着一层一层(一行一行)的填满的,自己拿草纸走一遍例子就行了....瞬间明白了...
  • 第二种方法恰恰和上面那种相反,它是不管别的,就看当前这一列能不能存水,也就是说是一列一列来的,比方说第二个0的位置,第一遍遍历的时候发现左边最高是1,第二遍遍历的时候发现右边最高的是3,那么不管两边是个什么情况,就在这一列都是可以蓄水为1的。

42. Trapping Rain Water [dp][stack]的更多相关文章

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

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

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

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

  3. LeetCode - 42. Trapping Rain Water

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

  4. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

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

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

  6. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  7. 刷题42. Trapping Rain Water

    一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...

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

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

  9. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

随机推荐

  1. java并发编程工具类JUC第一篇:BlockingQueue阻塞队列

    Java BlockingQueue接口java.util.concurrent.BlockingQueue表示一个可以存取元素,并且线程安全的队列.换句话说,当多线程同时从 JavaBlocking ...

  2. 使用BeautifulSoup高效解析网页,再也不用担心睡不着觉了

    BeautifulSoup是一个可以从 HTML 或 XML 文件中提取数据的 Python 库 那需要怎么使用呢? 首先我们要安装一下这个库 1.pip install beautifulsoup4 ...

  3. Vue之前后端交互

    Vue之前后端交互 一.前后端交互模式 接口调用方式 原生ajax 基于jQuery的ajax fetch axios 异步 JavaScript的执行环境是「单线程」 所谓单线程,是指JS引擎中负责 ...

  4. TVM图优化与算子融合

    TVM图优化与算子融合 计算图的定义 Computational graphs: a common way to represent programs in deep learning framewo ...

  5. NVIDIA数据中心深度学习产品性能

    NVIDIA数据中心深度学习产品性能 在现实世界的应用程序中部署AI,需要训练网络以指定的精度融合.这是测试AI系统的最佳方法-准备将其部署在现场,因为网络随后可以提供有意义的结果(例如,对视频流正确 ...

  6. 在Yolov5 Yolov4 Yolov3 TensorRT 实现Implementation

    在Yolov5 Yolov4 Yolov3 TensorRT 实现Implementation news: yolov5 support 引论 该项目是nvidia官方yolo-tensorrt的封装 ...

  7. kali2020.4中安装nessus 8.14.0

    1.下载软件包 官网下载地址:https://www.tenable.com/downloads/nessus 2.安装nessus dpkg -i /root/Nessus-8.14.0-debia ...

  8. ES6中的数组常用方法

    数组在JS中虽然没有函数地位那么高,但是也有着举足轻重的地位,下面我就结合这ES5中的一些常用的方法,与ES6中的一些方法做一些说明和实际用途.大家也可以关注我的微信公众号,蜗牛全栈. 一.ES5中数 ...

  9. 从零开始学架构(三)UML建模

    文章大纲 1.  文章介绍 2.  UML概述 3.  静态模型 4.  动态模型 5.  UML建模的一般过程 一.文章介绍 1.1为什么学习UML (1)UML是一种软件架构的模型表现方法,用于项 ...

  10. 《Docker基础与实战,看这一篇就够了》

    什么是Docker? Docker 使用 Google 公司推出的 Go 语言 进行开发实现,基于 Linux 内核的 cgroup,namespace,以及 AUFS 类的 Union FS 等技术 ...