leetcode 之trap water(8)

这题不太好想。可以先扫描找到最高的柱子,然后分别处理两边:记录下当前的局部最高点,如果当前点小于局部最高点,加上,
反则,替换当前点为局部最高点。
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)的更多相关文章
- [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] 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]Trapping Rain Water @ Python
原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- [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 ...
- Leetcode Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 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 ...
- LeetCode 755. Pour Water
原题链接在这里:https://leetcode.com/problems/pour-water/description/ 题目: We are given an elevation map, hei ...
随机推荐
- 【POJ2891】Strange Way to Express Integers(拓展CRT)
[POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream ...
- unity直连android真机在Profiler性能分析测试
基础步骤: 1.Unity打开你要测试的项目:File–Build Settings 2.如下图,按图顺序进行1.2.3.4.5操作,如果做过了,2就是灰色的,不能被点击,4和5需要相对应. 3.确保 ...
- python学习笔记(五) 200行实现2048小游戏
用前文提到的基础知识,和网上流行的2048源码,用python实现该游戏. 先将用户操作和游戏逻辑绑定. WASD分别对应移动方向上.左.下.右 然后实现矩阵的转置和逆置,这样只要实现一个方向的移动, ...
- 【题解】【LibreOJ Round #6】花团 LOJ 534 时间线段树分治 背包
Prelude 题目链接:萌萌哒传送门(/≧▽≦)/ Solution 如果完全离线的话,可以直接用时间线段树分治来做,复杂度\(O(qv \log q)\). 现在在线了怎么办呢? 这其实是个假在线 ...
- discuz uc_server 配置登录
新运行uc_server环境,先配置好ucenter链接-----这部很重要,我从新环境中安装下载的discuz代码,这部没配置,密码又不知道,怎么更改调试,都不起作用,在框架中,跳转到了原来线上的u ...
- 「Linux」制作一个Linux系统
一.前言 Linux操作系统至1991年10月5日诞生以来,就其开源性和自由性得到了很多技术大牛的青睐,每个Linux爱好者都为其贡献了自己的一份力,不管是在Linux内核还是开源软件等方面,都为我们 ...
- 「Python」python绘制图表
介绍一种简单而又功能强大的绘制图形或报表的包—pyecharts,一个基于Echarts(基于JS的数据可视化库)的图标类库,除了绘制常见的折线图.柱状图.饼图.箱型图和散点图外,还可以绘制3D柱状图 ...
- McNemar test麦克尼马尔检验
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...
- CSS3笔记-加强版
属性选择器: E[attr]只使用属性名,但没有确定任何属性值 E[attr="value"]指定属性名,并指定了该属性的属性值 E[attr~="value&quo ...
- node.js的安装配置——前端的配置
最近琢磨了以下node.js的安装,npm的配置,使用gulp watch监听index.html文件的修改,利用服务器打开网页. 打开自己写的网页不要本地双击打开,这样打开的网址是file:///E ...