Leetcode42. 接雨水
做法
考虑单独一列能生产多少贡献:用左右最大值去接这列的水
\(O(n)\)
Code
class Solution {
public:
int mx[1000000],rx[1000000];
int trap(vector<int>& height) {
int n(height.size());
// int mx(0);
for(int i=0;i<n;++i){
if(i==0) mx[i]=height[i];
else mx[i]=max(mx[i-1],height[i]);
}
int ans(0);
for(int i=n-1;i>=0;--i){
if(i==n-1) rx[i]=height[i];
else rx[i]=max(rx[i+1],height[i]);
ans+=min(rx[i],mx[i])-height[i];
}
/*int ans(0);
for(int i=0;i<n;++i){
ans+=min(rx[i],mx[i])-height[i];
}*/
return ans;
}
};
Leetcode42. 接雨水的更多相关文章
- [Swift]LeetCode42. 接雨水 | Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode---42. 接雨水 (hard)
题目:42. 接雨水 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 示例: 输入:height = [0,1,0,2,1,0,1,3,2,1,2, ...
- LeetCode42. 接雨水(java)
42.接雨水 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种 ...
- leetcode 日常清单
a:excellent几乎一次ac或只有点小bug很快解决:半年后再重刷: b:经过艰难的debug和磕磕绊绊或者看了小提示才刷出来: c:经过艰难的debug没做出来,看答案刷的: 艾宾浩斯遗忘曲线 ...
- 莫逸风CSDN文章目录
『Ⅱ』-----随笔 莫逸风CSDN文章目录 The Programmer's Oath程序员的誓言-- 今天突发奇想写了一个小工具,CSDN文章目录生成器 vue去掉一些烦人的校验规则 输入npm ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- lintcode:接雨水
接雨水 给出 n 个非负整数,代表一张X轴上每个区域宽度为 1 的海拔图, 计算这个海拔图最多能接住多少(面积)雨水. 如上图所示,海拔分别为 [0,1,0,2,1,0,1,3,2,1,2,1], 返 ...
- *42. Trapping Rain Water 接雨水
1. 原始题目 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这 ...
- LeetCode.接雨水
题外话:LeetCode上一个测试用例总是通不过(我在文章末贴出通不过的测试用例),给的原因是超出运行时间,我拿那个测试用例试了下2.037ms运行完.我自己强行给加了这句: && m ...
随机推荐
- Hibernate中Session.get()/load()之区别
原文链接http://sunxin1001.iteye.com/blog/292090 Session.load/get方法均可以根据指定的实体类和id从数据库读取记录,并返回与之对应的实体对象.其区 ...
- adb server is out of date. killing... ADB server didn't ACK * failed to start daemon *……
问题 使用 adb 命令的时候报错如下: adb server is out of date. killing... ADB server didn't ACK * failed to start d ...
- 转 Python3 ssl模块不可用的问题
编译安装完Python3之后,使用pip来安装python库,发现了如下报错: $ pip install numpy pip is configured with locations tha ...
- Centos7机器信息查看
1.查看Linux信息 1.1查看32位或64位 uname -a 1.2查看内核版本 cat /proc/version 1.3查看发行版 cat /etc/centos-release 2.查看内 ...
- 自动网页截图并指定元素位置裁剪图片并保存到excel表格
# coding=utf-8 import os import time from selenium import webdriver from selenium.webdriver.chrome.o ...
- 云计算第二阶段shell脚本
pstree #查看进程树 cat /etc/shells #查看系统安装的所有shell解释器 yum -y install ksh ...
- C++(四十三) — 函数模板机制
1.普通函数与模板函数调用原则 函数模板可以像普通函数一样被重载: 当函数模板和普通函数都符合条件时,编译器优先考虑普通函数: 但如果函数模板产生一个更好的匹配,则选择函数模板: 可以通过空模板实参 ...
- 科普文:Node.js 如何上传文件到后端服务【转】
原文链接 https://www.yuque.com/egg/nodejs/httpclient-upload 背景 互联网时代,无数服务是基于 HTTP 协议进行通信的. 除了常见的 前端浏览器 - ...
- 搭建MySQL MMM高可用
搭建MMM: 1,安装 agent 节点执行 yum install -y mysql-mmm-agent 2, monitor 节点执行 yum install -y mysql-mmm-monit ...
- Linux关闭防火墙、设置端口
关闭防火墙 1)重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 验证防火墙是否关闭:chkconfig --list |grep ...