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 ...
随机推荐
- 【BZOJ2763】飞行路线(最短路)
[BZOJ2763]飞行路线(最短路) 题面 BZOJ Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标 ...
- bzoj3192: [JLOI2013]删除物品(树状数组)
既然要从一个堆的堆顶按顺序拿出来放到第二个堆的堆顶,那么我们就可以把两个堆顶怼在一起,这样从一个堆拿到另一个堆只需要移动指针就好了. 换句话说,把1~n倒着,n+1到n+m正着,用一个指针把两个序列分 ...
- 【bzoj2006】【NOI2015】超级钢琴
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4292 Solved: 2195[Submit][Statu ...
- move_base代码学习一
System overview move_base 源码 API nav_core BaseGlobalPlanner BaseLocalPlanner RecoveryBehavior Recove ...
- jdk1.5后枚举类的定义规则
转: http://blog.csdn.net/willcold/article/details/12844487 JDK1.5 新增的enum关键字用于定义枚举类 枚举类也 ...
- Codeforces 833B The Bakery dp线段树
B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- git grep mysql 操作历史
history |grep mysql-----git history匹配出mysql操作的命令 !626 到mysql命令安装处链接mysql /usr/local/mysql/bin/mysql ...
- Kubernetes的Endpoints
在之前的博文中,我们演示过如何通过ceph来实现kubernetes的持久存储,以使得像mysql这种有状态服务可以在kubernetes中运行并保存数据.这看起来很美妙,然而在实际的生产环境使用中, ...
- 「Django」rest_framework学习系列-API访问跨域问题
#以中间件方式解决API数据访问跨域问题1.API下新建文件夹下写PY文件a.引入内置类继承: from django.middleware.common import MiddlewareMixin ...
- 前端PHP入门-019-内置函数之数学函数-很重要
查看帮助文档为主 函数名 描述 实例 输入 输出 abs() 求绝对值 $abs = abs(-4.2); //4.2 数字 绝对值数字 ceil() 进一法取整 echo ceil(9.999); ...