LeetCode之Trapping Rain Water
来源:互联网 发布:js传值input 编辑:程序博客网 时间:2024/06/05 19:11
/*方法一:对于每个柱子A[i],假设它左右边最高的柱子分别为A[l]和A[r],那么对于i柱子存储的水为max(A[l], A[r]) - A[i]。可以采用递归的方法求解每个柱子的左右最大值:1.从左往右扫描,获取每个柱子左边柱子中的最大值;2.从右往左扫描,获取每个柱子右边柱子中的最大值;方法参考自: https://github.com/soulmachine/leetcode*/class Solution {public: int trap(int A[], int n) { vector<int> left_max(n, 0); vector<int> right_max(n, 0); //从左往右扫描 for(int i = 1; i < n; ++i) left_max[i] = max(left_max[i-1], A[i-1]); //从右往左扫描 for(int i = n-2; i >= 0; --i) right_max[i] = max(right_max[i+1], A[i+1]); int res(0); for(int i = 1; i < n-1; ++i) res += min(left_max[i], right_max[i]) > A[i] ? min(left_max[i], right_max[i]) - A[i] : 0; return res; }};
0 0
- leetcode之Trapping Rain Water
- leetcode之Trapping Rain Water
- LeetCode之Trapping Rain Water
- Leetcode之Trapping Rain Water
- 【Leetcode】之Trapping Rain Water
- LeetCode: Trapping Rain Water
- LeetCode : Trapping Rain Water
- [LeetCode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- [LeetCode]Trapping Rain Water
- LeetCode-Trapping Rain Water
- [leetcode] Trapping Rain Water
- [LeetCode] Trapping rain water
- Leetcode: Trapping Rain Water
- Leetcode:Trapping Rain Water
- [LeetCode] Trapping Rain Water
- <Leetcode>Trapping Rain Water
- 三层架构的电力系统
- 移动WEB前段框架
- IOS 手写UICollectionView
- ResharperSetup.8.0.2完美破解版 支持VS2013
- rfc2459
- LeetCode之Trapping Rain Water
- 51单片机寄存器功能一览表
- bzoj2561: 最小生成树 最大流
- jQuery 中 $(document).ready()事件与js中window.onload()事件的区别
- Android OSD层 “挖洞”显示视频层
- Android开发之自定义Notification
- DLL动态链接库编程入门之二:非MFC DLL
- web.py源码分析: application
- 关于$_REQUEST['GLOBALS']的一些分析