LeetCode 42. Trapping Rain Water–c++解法


LeetCode题解专栏:LeetCode题解
LeetCode 所有题目总结:LeetCode 所有题目总结
大部分题目C++,Python,Java的解法都有。


题目地址:


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.

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. Thanks Marcos for contributing this image!

Example:

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

这道题目跟第84题很类似,没做的可以先看这篇文章:LeetCode hard 84. Largest Rectangle in Histogram–python,java 15行,c++ 15行 解法


这道题目是非常经典的一道题目,我记得字节跳动和百度都出过这道编程题。
最容易想到的是求两边的最大值,然后比较,时间复杂度为O(n)。
C++解法如下:

class Solution {public:int trap(vector<int> &height) {if (height.empty()){return 0;}int ans = 0;int size = height.size();vector<int> left_max(size), right_max(size);left_max[0] = height[0];for (int i = 1; i < size; i++) {left_max[i] = max(height[i], left_max[i - 1]);}right_max[size - 1] = height[size - 1];for (int i = size - 2; i >= 0; i--) {right_max[i] = max(height[i], right_max[i + 1]);}for (int i = 1; i < size - 1; i++) {ans += min(left_max[i], right_max[i]) - height[i];}return ans;}
};

c++两边往中间夹的解法:

class Solution {public int trap(int[] height) {// time : O(n)// space : O(1)if (height.length==0) return 0; int left = 0, right = height.length-1; int leftMax=0, rightMax=0; int ans = 0; while (left < right) {if (height[left] > leftMax) leftMax = height[left]; if (height[right] > rightMax) rightMax = height[right];if (leftMax < rightMax) {ans += Math.max(0, leftMax-height[left]); left++; } else {ans += Math.max(0, rightMax-height[right]); right--; }}return ans; }
}

LeetCode 42. Trapping Rain Water--算法题--c++解法相关推荐

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

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

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

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

  3. 【重点:DP 双指针 栈】LeetCode 42. Trapping Rain Water

    LeetCode 42. Trapping Rain Water 本博客转载自:http://www.cnblogs.com/grandyang/p/4402392.html [自己又不会做,抄的-& ...

  4. LeetCode 42 Trapping Rain Water 收集雨水

    LeetCode 42 Trapping Rain Water 收集雨水 Given n non-negative integers representing an elevation map whe ...

  5. LeetCode 42. Trapping Rain Water(收集雨水Ⅰ)

    题目描述: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...

  6. [LeetCode]42. Trapping Rain Water雨水填坑

    这个题难点在于无法保证右边是不是有更高的墙可以保证挡住水 双指针可以解决 /*两边指针保证,保证另外一边肯定有能挡住水的地方.如果从一边开始,不考虑另一边,是无法保证右边肯定有挡水的墙,如果右边只都比 ...

  7. Leetcode 407. Trapping Rain Water II 收集雨水2 解题报告

    1 解题思想 我看了下题目,发现比预想中的简单,加之我比较烂,所以其实我还是没做,只是看懂了上回贴的代码,然后做了一下注释,现在我来讲下题目. 首先请看下上一题,上一题是2D的这题是3D的: Leet ...

  8. 【重点:BFS】LeetCode 407. Trapping Rain Water II

    LeetCode 407. Trapping Rain Water II 博客转载自:http://www.cnblogs.com/grandyang/p/5928987.html [太难了,被智商碾 ...

  9. Leetcode 动态规划 Trapping Rain Water

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Trapping Rain Water Total Accepted: 14568 Tota ...

最新文章

  1. mybatis与php,浅谈mybatis中的#和$的区别
  2. p187让元素垂直居中
  3. 利用python卷积神经网络手写数字识别_Keras深度学习:卷积神经网络手写数字识别...
  4. 面向对象——单例设计模式
  5. 关于三维莫队问题的一些思考和探究
  6. 1092 最好吃的月饼 (20分)_24行代码AC
  7. leetcode 73. 矩阵置零
  8. 70. Climbing Stairs【leetcode】递归,动态规划,java,算法
  9. spss练习数据_动手练习SPSS因子分析啦,会不会做一动手就知道了
  10. Exchange Server 2010 LAB Part6.边缘传输服务器部署和应用
  11. 深入解读Linux内存管理系列(3)——MMU初始化和页表的建立
  12. Atitit 增强代码健壮性 出错继续执行恢复模式,就像vbs那样我以为我可以使用Try/Catch,但是我找不到异常后是否可以继续执行代码,并且找不到如何在最后显示错误消息。目录PHP
  13. web前端三大主流框架分析对比
  14. 地质专业考遥感计算机研究生,我想考中国地质大学的研究生,谁能告诉我是选遥感..._在职考研_帮考网...
  15. The POM for is missing, no dependency information available
  16. Outlook 365 添加企业Exchange邮箱(亲测)
  17. 发送短信验证码时触发天级流控Premit:10
  18. 论以建筑全生命周期管理建设公司大数据平台
  19. 计算机一级考试《MS Office》
  20. macOS 应用崩溃日志

热门文章

  1. RDChiral | 用于处理立体化学的RDKit封装器
  2. centos7 选定默认启动内核,及删除无用内核
  3. python 内推_用Python实现内推外插法
  4. 癌症中克隆种群结构统计推断分析软件PyClone安装小记
  5. mSystems:干旱对土壤微生物组的影响
  6. R语言ggplot2可视化:使用geom_line函数将dataframe中数据可视化为时间序列(或折线图)(Time Series Plot From a Data Frame)、添加标题、副标题
  7. pandas使用groupby函数和agg函数获取每个分组特定变量独特值的个数(number of distinct values in each group in dataframe)
  8. R语言基于多字段(多数据列、multiple columns)对dataframe的行数据进行排序(Ordering rows)实战:使用R原生方法、data.table、dplyr等方案
  9. 为Jupyter notebook配置R kernel过程及踩坑记录
  10. python代码读取文件并将文件反序存入另外一个文件