博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode – Refresh – Trapping Rain Water
阅读量:6869 次
发布时间:2019-06-26

本文共 976 字,大约阅读时间需要 3 分钟。

Scanning from left to right and right to left.

Find the max height of left and right. The minimum one will be most possible height for current state that can bound the water. But one thing need to check:

whether current height is higher than the value.

1 class Solution { 2 public: 3     int trap(int A[], int n) { 4         if (n < 3) return 0; 5         vector
left(n), right(n); 6 int result = 0, rec = A[0]; 7 for (int i = 1; i < n; i++) { 8 left[i] = max(rec, left[i-1]); 9 rec = max(rec, A[i]);10 }11 rec = A[n-1];12 for (int i = n-2; i >= 0; i--) {13 right[i] = max(rec, right[i+1]);14 rec = max(rec, A[i]);15 }16 for (int i = 0; i < n; i++) {17 result += (min(left[i], right[i]) - A[i]) > 0 ? (min(left[i], right[i]) - A[i]) : 0;18 }19 return result;20 }21 };

 

转载于:https://www.cnblogs.com/shuashuashua/p/4363374.html

你可能感兴趣的文章
自动化运维之善用zabbix监控网站下载速度
查看>>
我的友情链接
查看>>
碎碎的笔记(二)
查看>>
我的友情链接
查看>>
Unity手动调用物理引擎Update
查看>>
linux 命令
查看>>
JAVA8新特性之:Stream 详解
查看>>
RHEL vsftpd多个虚拟用户访问不同目录问题
查看>>
CENTOS7 Python3.7 为jupyter notebook 安装python2.7内核
查看>>
control userpasswords2实现xp的自动登陆
查看>>
CKEDITOR使用与配置
查看>>
Linux课程第十六天学习笔记
查看>>
Redis作者谈Redis应用场景
查看>>
数据库外键的使用以及优缺点
查看>>
解决oracle set auto trace on 错误
查看>>
Step2:Apply NLS patch
查看>>
jsp---语句对象Statement
查看>>
java进阶之路
查看>>
优化Android Studio
查看>>
zabbix二次开发-flask-获取告警
查看>>