Trapping Rain Water [LeetCode]
Problem Description: http://oj.leetcode.com/problems/trapping-rain-water/
Basic idea: Get the index of max number of the array, then split the array at that point. Find the left max num, calcaute the water trapped between left max num and the max num. Go left recursively until meeting the start of the array. Do the same thing to the right part.
class Solution {
public:
int findMax(int A[], int n) {
int max = A[];
int max_index = ;
for(int i = ; i < n; i++) {
if (A[i] > max){
max = A[i];
max_index = i;
}
}
return max_index;
} int trap(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(n <= )
return ; int max_index = findMax(A, n);
int water = ; int i = max_index;
while(i >= ){
int left_max_index = findMax(A, i - + );
//calculate water between left_max and max
for(int j = left_max_index + ; j < i; j ++)
water += A[left_max_index] - A[j]; i = left_max_index;
} i = max_index;
while( n - (i + ) >= ){
int right_max_index = findMax(A + i + , n - i - ) + i + ;
//calculate water between right_max and max
for(int j = i + ; j < right_max_index; j++)
water += A[right_max_index] - A[j]; i = right_max_index;
} return water;
}
};
Trapping Rain Water [LeetCode]的更多相关文章
- Trapping Rain Water leetcode java
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- [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][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- [LeetCode] 接雨水,题 Trapping Rain Water
这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
随机推荐
- 0(Mark)(随时添加) ubuntu的一些 终端 命令整理
MARK 1 查看cpu信息cat /proc/cpiinfo 2 查看ubuntu版本:cat /etc/issue 3 查看系统是32位还是64位方法1:#查看long的位数,返回32或64 ge ...
- Download file using libcurl in C/C++
http://stackoverflow.com/questions/1636333/download-file-using-libcurl-in-c-c #include <stdio.h&g ...
- MS16-016 提权EXP
测试环境 win7 32 位未打任何补丁 1.使用net user 指令 测试得到结果没有权限新建用户 2.查看系统用户 3.复制EXP到win7 下 使用命令打开 添加新用户再查看用户列表
- CG绘画笔记
看一些比较好的作品:看作品哪些部分,部件,盔甲比较吸引人,提取一个比较好的点,进行组合创作. 逆光.切光布局构图 创作:故事.情感.经历.朋友.时代 灵感: 电影.音乐 变化(色彩)透视 空气透视视觉 ...
- python strip()函数
转发:jihite-博客园-python strip()函数 函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除序列的 ...
- hdu 3033 I love sneakers! 分组背包
I love sneakers! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- h5移动开发css
最近刚开始做移动端的开发,接触到很多新的东西,很荣幸(*^__^*) , 下面我们开始正式介绍最近新接触到的属性啦,一起进步: 1.点击按钮等会产生阴影,可设置这个属性:-webkit-tap-hig ...
- MySQL数据表生成ER图
遇到没有说明文档的数据库的时候,一张一张表去分析,需要花费很长时间和精力. 幸好有一些工具,可以帮助我们生成ER图,这样看起来就一目了然: 下面我将自己的一次实践记录于下,供参考: 1.下载并安装工具 ...
- [转载] iptables配置实践
原文: http://wsgzao.github.io/post/iptables/ iptables配置实践 By wsgzao 发表于 2015-07-24 文章目录 1. 前言 2. 更新历史 ...
- DOM加载:浏览器渲染和操作顺序(转载 学习中。。。)
DOM加载:浏览器渲染和操作顺序 1.HTML解析完毕 2.外部脚本和样式表加载完毕 3.脚本在文档内解析并执行 4.HTML DOM完全构造起来 5.图片和外部内容加载 6.网页完成加载 基于这个顺 ...