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 ...
随机推荐
- Windows上能看朋友圈的微信来了 | 附下载地址
昨天的时候,电脑端的微信提示更新就顺手更新了一下,更新完成后习惯性的点了下设置,纳尼,居然被灰到了测试版本? 带着好奇,赶快看了下更新了什么内容: 支持浏览朋友圈 "搜一搜"支持搜 ...
- SystemVerilog MCDF验证结构
MCDF的设计和验证花费的时间:(工作中假设的时间) design cycle time ==10days how about 验证?verify? 模块越往上(大')验证花费的时间越来越大,但是 ...
- PDF 文件编写器 C# 类库(版本 1.28.0)使用详解
PDF File Writer 是一个 C# .NET 类库,允许应用程序创建 PDF 文件. PDF File Writer C# 类库使 .NET 应用程序能够生成 PDF 文档.该库使应用程序免 ...
- 转载 | Scala 如何避免使用 Null
在 Java 里,null 是一个关键字,不是一个对象,所以对它调用任何方法都是非法的. Scala 的 Option类型 Scala在变量和函数返回值可能不会引用任何值的时候使用 Option 类型 ...
- Seata分布式事务框架Sample
前言 阿里官方给出了seata-sample地址,官方自己也对Sample提供了很多类型,可以查看学习. 我这里选择演示SpringBoot+MyBatis. 该聚合工程共包括5个module: sb ...
- Python“九九乘法表”
用Python语言编程,使用双重循环语句输出"九九乘法表". for i in range(1, 10): # 控制行 for j in range(1, i+1): # 控制列 ...
- thymeleaf+Springboot实现自定义标签
在项目开发中,有一些组件不能满足我们快速开发的要求,我们需要封装一些组件来更加的便利我们.比如,我们可以封装一个下拉框组件,只要开发人员只有引用这个组件的标签,就能出现效果,而不用再去请求url,渲染 ...
- 实战SpringBoot Admin
长话短说哦,直接查看下文吧 目录 声明 先锋 前提 SpringBoot Admin 介绍 服务端的搭建 客户端的搭建 参数的指南 尾声 声明 见名知意,实战SpringBoot Admin,实战!实 ...
- 狂神说JUC学习笔记(二)
狂神说JUC的原版笔记: 链接:https://pan.baidu.com/s/12zrGI4JyZhmkQh0cqEO4BA 提取码:d65c 我的笔记在狂神的笔记上增加了一些知识点或者做了些许补充 ...
- P1831 杠杆数(数位Dp)
题目描述 如果把一个数的某一位当成支点,且左边的数字到这个点的力矩和等于右边的数字到这个点的力矩和,那么这个数就可以被叫成杠杆数. 比如$4139$就是杠杆数,把3当成支点,我们有这样的等式:$4 \ ...