这题不太好想。可以先扫描找到最高的柱子,然后分别处理两边:记录下当前的局部最高点,如果当前点小于局部最高点,加上,

反则,替换当前点为局部最高点。

int trapWater(int A[], int n)
{
int peak = ;
int max = ;
int water = ;
for (int i = ; i < n; i++)
{
if (A[i]>A[max])max = i;
} for (int i = ; i < max; i++)
{
if (A[i]>peak)
peak = A[i];
else
water += peak - A[i];
} for (int j = n - ; j > max; j--)
{
if (A[j]>peak)
peak = A[j];
else
water += peak - A[j];
} return water;
}

leetcode 之trap water(8)的更多相关文章

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

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

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

  3. 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 ...

  4. [leetcode]Trapping Rain Water @ Python

    原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...

  5. LeetCode: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

  6. [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  7. Leetcode Trapping Rain Water

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

  8. Leetcode: Pacific Atlantic Water Flow

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  9. LeetCode 755. Pour Water

    原题链接在这里:https://leetcode.com/problems/pour-water/description/ 题目: We are given an elevation map, hei ...

随机推荐

  1. 【BZOJ1443】游戏(二分图匹配,博弈论)

    [BZOJ1443]游戏(二分图匹配,博弈论) 题面 BZOJ 题解 很明显的二分图博弈问题. 发现每次移动一定是从一个黑点到达一个白点,或者反过来. 所以可以对于棋盘进行染色然后连边. 考虑一下必胜 ...

  2. Mac连接HDMI后没有声音

    Mac连接HDMI后,会自动切换到HDMI设备进行发声,若HDMI设备没有声音,则不会发声.必须切换回内置扬声器才能有声音,或者拔出HDMI设备. 系统偏好设置 -  声音 - 输出 - 选择内置扬声 ...

  3. bzoj2006: [NOI2010]超级钢琴(堆+RMQ)

    和上一道题同类型...都是用堆求第k大 考虑对于每一个r,怎么求出一个最优的l.显然只需要求出前缀和,用RMQ查询前面最小的l的前缀和就好了.但是对于一个r,每个l只能选一次,选了一次之后,考虑怎么把 ...

  4. [ACM]Codeforces Round #534 (Div. 2)

    A. Splitting into digits Vasya has his favourite number n. He wants to split it to some non-zero dig ...

  5. 据说要写一个CTSC&APIO的收获

    就不写流水帐了,总的写一下吧.先从最浅显的地方开始——知识.大概被普及了一发带花树,算上自己的考试,还被普及了一发洲阁筛.当然更多的还是对于一些知识的强化,比如:乱搞(这东西真是太重点了啊).DP.数 ...

  6. 【翻译】InterlockedIncrement内部是如何实现的?

        Interlocked系列函数可以对内存进行原子操作,它是如何实现的?     它的实现依赖于底层的CPU架构.对于某些CPU来说,这很简单,例如x86可以通过LOCK前缀直接支持Interl ...

  7. oracle的sign()函数

    sign函数 比较大小函数 sign 函数语法:sign(n) 函数说明:取数字n的符号,大于0返回1, 小于0返回-1, 等于0返回0 示例1: ),),) from dual; ) ) ) ——— ...

  8. opencv查找轮廓---cvFindContours && cvDrawCountours 用法及例子

    http://blog.csdn.net/timidsmile/article/details/8519751 环境: vs2008 + opencv2.1 先看,这两个函数的用法(参考 opencv ...

  9. MFC之ListCtrl动态添加按钮

    先上效果图: 图中用了一个CListCtrl插件,隐藏了网格线(LVS_EX_GRIDLINES). 添加了“删除”按钮(按钮上贴了图片),选中哪一行则显示在哪一行. 有两种方式创建按钮,一种是直接根 ...

  10. Codeforces Round #336 (Div. 2)B 暴力 C dp D 区间dp

    B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input stan ...