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 ...
随机推荐
- Spring中使用要点集合
1.InitializingBean和init-method方法 Spring的InitializingBean为bean提供了定义初始化方法的方式.InitializingBean是一个接口,它仅仅 ...
- Codeforces707Div2
A Small, but very brave, mouse Brain was not accepted to summer school of young villains. He was ups ...
- loj2541 「PKUWC2018」猎人杀 【容斥 + 分治NTT】
题目链接 loj2541 题解 思路很妙啊, 人傻想不到啊 觉得十分难求,考虑容斥 由于\(1\)号可能不是最后一个被杀的,我们容斥一下\(1\)号之后至少有几个没被杀 我们令\(A = \sum\l ...
- 常用Actoin算子 与 内存管理 、共享变量、内存机制
一.常用Actoin算子 (reduce .collect .count .take .saveAsTextFile . countByKey .foreach ) collect:从集群中将所有的计 ...
- 【loj#139】树链剖分
#139. 树链剖分 题目描述 这是一道模板题. 给定一棵 $n$个节点的树,初始时该树的根为 111 号节点,每个节点有一个给定的权值.下面依次进行 $m$ 个操作,操作分为如下五种类型: 换根:将 ...
- Android开发-eclipse+phonegap(Cordova)环境搭建
搭建步骤: 一.安装java [官网下载].eclipse+ADT+Android SDK [点我下载x86(android-22)] | [adt-bundle-windows-x86_64-201 ...
- 使用 nginx 代理 tomcat 服务器
server { listen 80; server_name wechat-jsp.local; root /usr/local/Cellar/tomcat/9.0.5/libexec/webapp ...
- 基于epoll封装的事件回调miniserver
epoll技术前两节已经阐述过了,目前主要做一下封装,很多epoll的服务器都是采用事件回调方式处理, 其实并没有什么复杂的,我慢慢给大家阐述下原理. 在networking.h和networking ...
- 主席树 或者 离散化+分块 BZOJ 4636
4636: 蒟蒻的数列 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 381 Solved: 177[Submit][Status][Discuss ...
- jquery 事件对象属性小结
使用事件自然少不了事件对象. 因为不同浏览器之间事件对象的获取, 以及事件对象的属性都有差异, 导致我们很难跨浏览器使用事件对象. jQuery中统一了事件对象, 当绑定事件处理函数时, 会将jQue ...