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 ...
随机推荐
- 【PDF】HTML中嵌入pdf的简单方法
<embed src="> 或者你不想显示某些功能的话: <embed src=">
- Windows用户相关操作
获取所有用户 NET_API_STATUS NetUserEnum( LPCWSTR servername, DWORD level, DWORD filter, LPBYTE* bufptr, DW ...
- jsonP 后台写法 及 层级树型数据递归查询
Controller层: package com.taotao.rest.controller; import org.springframework.beans.factory.annotation ...
- OpenCV中响应鼠标消息 (转)
#include <cv.h> #include <highgui.h> #include <stdio.h> #pragma comment(lib," ...
- 简单shell 编程
简单shell编程 by dreamboy #!/bin/bash while true do echo clear echo echo " 系统维护菜单 " echo &qu ...
- git untrack file
git update-index should do what you want This will tell git you want to start ignoring the changes t ...
- linux根据进程pid查看进程详细信息
http://note.youdao.com/noteshare?id=af2fdd34e3adfacda2d34706e16e5045
- static final修饰的静态变量修改后更新到服务器,重启无法生效的问题
今天在工作中碰到这样一个问题,有一个常量类,将工程中常用的一些变量定义在了里面.今天我要修改其中的某个变量.修改完后将编译好的.class文件更新到了服务器上,但是重启服务器后发现始终没有变化,还是以 ...
- net-speeder
有的同学反映自己的搬瓦工速度慢,丢包率高.这其实和你的网络服务提供商有关.据我所知一部分上海电信的同学就有这种问题.那么碰到了坑爹的网络服务商,我们应该怎么办呢? duangduang~~~~~~有请 ...
- Codeforces 148 D Bag of mice
D. Bag of mice http://codeforces.com/problemset/problem/148/D time limit per test 2 seconds memory l ...