42. Trapping Rain Water [dp][stack]
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]的更多相关文章
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- 刷题42. Trapping Rain Water
一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
随机推荐
- easyui datagrid 自定义单元格单击与双击事件(Day_38)
$(function(){ $('#tableId').datagrid({//单击事件 onClickRow:function(rowIndex,rowData){ alert("单 ...
- SystemVerilog 激励发生器
simulator(激励发生器) 主要来的职责是模拟与DUT相邻设计的接口协议,只需要关注如何模拟接口信号,时期能够以真实的接口协议来发送激励给DUT. simolator不应该违反协议,但是不约束于 ...
- 为何不选择lunix AIO
对于块设备而言,linux可以使用同步IO.POSIX IO.linux AIO.io-uring,前俩者是linux的同步IO接口,后者是linux内核提供的异步io接口,linux AIO只支持直 ...
- 【排除解决】System.Runtime.InteropServices.ExternalException (0x80004005): GDI+ 中发生一般性错误
前言: 今天项目发布上线,发布到正式环境验证功能的时候忽然方向之前做的一个图片合成的功能报错了提示:System.Runtime.InteropServices.ExternalException ( ...
- 【greys使用】阿里greys在线诊断工具
Greys是一个Java进程的异常诊断工具,可以在不停止程序的前提下,对一些问题进行检测.这个框架主要是采用Java的探针技术,可以做到动态修改java的字节码技术.前提是Jdk版本6+.(prema ...
- Redis限制一键登录次数
一.产生背景 之前的随笔提到过项目中写了一键登录功能.上线后除了有时候网络波动会导致登陆失败,其他情况一直稳如老狗 しかし,邮件看到有人恶意刷一键登录,这年头闲的人可真闲啊, 只能思考如何搞一搞 二. ...
- Docker学习(9) Docker守护进程的配置和操作
- 面部表情视频中进行远程心率测量:ICCV2019论文解析
面部表情视频中进行远程心率测量:ICCV2019论文解析 Remote Heart Rate Measurement from Highly Compressed Facial Videos: an ...
- VB 老旧版本维护系列---兜兜转转有点晕:从服务器通过URL不中转保存的下载
从服务器通过URL不中转保存的下载 首先引用System.dll 然后新开一个页面,空的,在后台Page_Load方法里写 Dim docPath As String ="" ...
- 【NX二次开发】打开信息窗口UF_UI_open_listing_window
头文件:uf_ui_ugopen.h函数名:UF_UI_open_listing_window 函数说明:打开信息窗口 测试代码: #include <uf.h> #include < ...