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),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
随机推荐
- jquery之html(),text()方法详解
一:html() html()函数用于设置或返回当前jQuery对象所匹配的DOM元素内的html内容. 该函数的用途相当于设置或获取DOM元素的innerHTML属性值. 该函数属于jQuery对象 ...
- [Effective Java]第十章 并发
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- react入门笔记
this.props.children是任何内嵌的元素 利用ref属性给子组件命名,this.refs引用组件,getDOMNode()获取本地的DOM元素,如: this.refs.author.g ...
- [转载] 理解 rto
原文: http://mp.weixin.qq.com/s?__biz=MzAxNjM3MDkyOQ==&mid=204656491&idx=1&sn=5046aa16eee0 ...
- 关于css3的自定义字体
css3的@font-face属性打破了中文页面字体一成不变的格局,但今天本菜在用的时候并不那么爽.开始各种引用外部ttf文件失败.下了300M+的字体文件,苦逼的试了一下午.终于有一个ttf引用成功 ...
- 高处胜寒 php中奖概率算法,可用于刮刮卡,大转盘等抽奖算法
<?php /* * 经典的概率算法, * $proArr是一个预先设置的数组, * 假设数组为:array(100,200,300,400), * 开始是从1,1000 这个概率范围内筛选第一 ...
- js中event.keyCode用法及keyCode对照表
HTML 用户名:<input type="text" id="UserAccount" onKeyPress="JumpByEnter(Use ...
- MFC编程入门之六(对话框:创建对话框模板和修改对话框属性)
本节开始为大家讲解偏向应用的知识--创建对话框. 对话框,大家应该很熟悉了,在我们常用的软件中大多都有对话框界面,例如,360安全卫士的主界面其实就是对话框,知识它做了很多美工方面的工作,将其大大美 ...
- composer 安装yii插件 fontawesome
国外站点 http://fontawesome.io/ 国内站点 http://fontawesome.dashgame.com/ Installation The preferred way to ...
- 简析 addToBackStack使用和Fragment执行流程
在使用Fragment的时候我们一般会这样写: FragmentTransaction transaction = getSupportFragmentManager().beginTransacti ...