Trapping Rain Water (Bar Height) -- LeetCode
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. Thanks Marcos for contributing this image!
思路:使用两个指针left和right,分别从两端向中间靠拢。同时,记录left和right曾经到过的最高值。如果当前值并没有超过最高值,则将最高值与当前的差值作为水的量,并将指针加一(right指针是减一)。为了让这个方法有效(无效的情况是,指针后续遇见的bar都不会再高于最高值了,那么这些水根本存不起来),因此我们每次只移动left和right中矮的那一个(如果两者相等,则移动left)。这样,一定能保证最后会碰到比最高值要高的bar(至少是等高),因为我们移动的是矮的那个指针,往前移动肯定最后会遇见另一个高的指针。
class Solution {
public:
int trap(vector<int>& height) {
if (height.size() == ) return ;
int left = , right = height.size() - , res = ;
int maxleft = height[left], maxright = height[right];
while (left < right)
{
if (height[left] <= height[right])
{
if (height[left] > maxleft) maxleft = height[left];
else res += maxleft - height[left];
left++;
}
else
{
if (height[right] > maxright) maxright = height[right];
else res += maxright - height[right];
right--;
}
}
return res;
}
};
bar height问题:这是Amazon面试中的一道问题。求最高的液面高度。这里只需要把上方的代码修改一下就可以用。就是在res值变更时,若max与当前高度差值非零时,记录下max值,最后最高的max值就是结果。
class Solution {
public:
int trap(vector<int>& height) {
if (height.size() == ) return ;
int left = , right = height.size() - , res = ;
int maxleft = height[left], maxright = height[right];
while (left < right)
{
if (height[left] <= height[right])
{
if (height[left] > maxleft) maxleft = height[left];
else if (maxleft - height[left] > )
res = max(res, maxleft);
left++;
}
else
{
if (height[right] > maxright) maxright = height[right];
else if (maxright - height[right] > )
res = max(res, maxright);
right--;
}
}
return res;
}
};
Trapping Rain Water (Bar Height) -- LeetCode的更多相关文章
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- [LeetCode] 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的五种解法详解
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: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
随机推荐
- 剑指offer-替换空格02
题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. class Solution: ...
- Android之SQLite总结
SQLite 是一个轻量级的数据库,常用于各种嵌入式设备当中.android 提供了SQLiteOpenHelper的抽象类用于帮助开发数据库.在实际使用中经常定义一个类继承SQLiteOpenHel ...
- JavaWeb笔记(九)Ajax&Json
AJAX 实现方式 原生的JS实现方式 //1.创建核心对象 var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, C ...
- [译]如何禁止Requests库的log日志信息呢?
原文来源: https://stackoverflow.com/questions/11029717/how-do-i-disable-log-messages-from-the-requests-l ...
- Android动态添加和移除布局
package com.hyang.administrator.studentproject; import android.os.Bundle; import android.support.v7. ...
- vmware中三种网络连接方式(复制)
原文来自http://note.youdao.com/share/web/file.html?id=236896997b6ffbaa8e0d92eacd13abbf&type=note 我怕链 ...
- PHP路径相关 dirname,realpath,__FILE__
比如:程序根目录在:E:\wamp\www 中 1. __FILE__ 当前文件的绝对路径 如果在index.php中调用 则返回 E:\wamp\www\index.php 下面再看一 ...
- 201621123033 《Java程序设计》第1周学习总结
1. 本周学习总结 · jdk.jre.jvm的含义及相关概念(具体见下文回答) · 会使用记事本及简单编辑器编写Java程序,理解javac和java命令的含义(具体见下文回答) · java与C语 ...
- 【Nescafé 31】杯NOIP模拟赛
t1 题意:n*m的棋盘上从(1,1)走到(n,m),只能向下或向右,一些格子有老鼠,每个老鼠互不相同,当处于与老鼠有重边的格子时,视为看见了这只老鼠,求到终点看到最少的不同老鼠数. 分析:DP 由于 ...
- 【Luogu】P3239亚瑟王(概率DP)
题目链接 请看luogu第一篇题解 #include<cstdio> #include<algorithm> #include<cstring> #include& ...