题目

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!

代码

class Solution {
public:
int trap(vector<int>& height) {
// non valid input
int len = height.size();
if (len<=) return ;
// initial
int left_max[height.size()];
int right_max[height.size()];
left_max[] = ;
right_max[len-] = ;
// get left_max and right_max
for (int i = ; i < len; ++i)
{
left_max[i] = std::max(left_max[i-], height[i-]);
right_max[len-i-] = std::max(right_max[len-i], height[len-i]);
}
// calculate the sum
int sum = ;
for (int i = ; i < len; ++i)
{
int h = std::min(left_max[i], right_max[i]);
if (h>height[i])
{
sum += h-height[i];
}
}
return sum;
}
};

Tips:

1. 遍历,获得每个位置上左边最高的和右边最高的;选择左边和右边比较小的高度,减去该位置的高度,就是可需水量。

2. 注意一些极端case的处理

=================================================

第二次过这道题,思路没有完全记清,稍微捡了一下思路,一次AC。

class Solution {
public:
int trap(vector<int>& height) {
if ( height.size()< ) return ;
// left height
vector<int> l(height.size(),);
int l_heighest = ;
for ( int i=; i<height.size(); ++i )
{
l_heighest = std::max(l_heighest, height[i]);
l[i] = l_heighest;
}
// right height
vector<int> r(height.size(),);
int r_heighest = ;
for ( int i=height.size()-; i>=; --i )
{
r_heighest = std::max(r_heighest, height[i]);
r[i] = r_heighest;
}
// total trapping water
int ret = ;
for ( int i=; i<height.size(); ++i )
{
int h = std::min(l[i], r[i]);
ret += h - height[i];
}
return ret;
}
};

tips:

这道题就是一句话口诀:左右短的,减去当前位置高度,等于当前位置可需水量。

【Trapping Rain Water】cpp的更多相关文章

  1. leetcode 【 Trapping Rain Water 】python 实现

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

  2. 【LeetCode】42. Trapping Rain Water

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

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

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

  4. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  5. [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 ...

  6. [LeetCode] Trapping Rain Water 收集雨水

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

  7. [LintCode] Trapping Rain Water 收集雨水

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

  8. LeetCode - 42. Trapping Rain Water

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

  9. 有意思的数学题:Trapping Rain Water

    LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...

随机推荐

  1. JS浏览器获取宽高

    screen.availHeight is the height the browser's window can have if it is maximized. (including all th ...

  2. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:10.项目介绍之架构(2)

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 前言 前面我们讲到<迷你微信>服务器端的主架构,现在我们来描述一下它的模块详细信息. 网络模块 从上图我 ...

  3. python logging 模块记录日志

    #日志记录到多文件示例 import logging def error_log(message): file_1_1 = logging.FileHandler('error.log', 'a+', ...

  4. mybatis 异常处理:Invalid bound statement (not found)

    mybatis 的使用过程中提示错误: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): ...

  5. 避免使用 JS 特性 with(obj){}

    1)简要说明         with 语句可以方便地用来引用某个特定对象中已有的属性,但是不能用来给对象添加属性.要给对象创建新的属性,必须明确地引用该对象. 2)语法格式  with(object ...

  6. ES6中set和map的区别

    Set ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set函数可以接受一个数组(或类似数组的对象)作为参数,用来初始化. // 例一 var set = ne ...

  7. hdu-2112 HDU Today---dijkstra+标号

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2112. 题目大意: 求起点到终点的最短路 解题思路: 对地名进行编号即可 然后直接dijkstra算 ...

  8. 【转】CentOS 7.0 安装Redis 3.2.1详细过程和使用常见问题

    http://www.linuxidc.com/Linux/2016-09/135071.htm 环境:CentOS 7.0 Redis 3.2.1 Redis的安装与启动 这里我把Redis放在/h ...

  9. DOTA自走棋卡牌及搭配阵容

    这个游戏其实就根炉石jjc和A牌轮抽一样,前期要找着质量牌抓,保证你至少不漏.根据你的需求补一些你不会上场的阵容组件,最后根据你的组件和核心紫卡来哪张来决定打什么.另外也要考虑场上另外几家,如果有一家 ...

  10. json代码驾照考题批量加入MySQL数据库 ps.executeUpdate()永远只能悲催的加一条数据 去掉id主键自增 用foreach循环数据库只能出现一条语句

    package com.swift; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStrea ...