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),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
随机推荐
- 图片轮播的JS写法,通用涉及多个轮播
本代码是借鉴大神的代码分析理解后,自己改写的!有不足指出希望给为大神指点. 核心只有一个JS,里面包含了css样式. 展示效果图:
- Python-爬虫初学
#爬取网站中的图片 1 import re #正则表达式库 import urllib #url链接库 def getHtml(url): page = urllib.urlopen(url) #打开 ...
- spring的自动装配(default-autowire="byName")
自动装配,官方给出的定义是这样:Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系.因此,如果可能的话,可以自 动让Spring通过检查BeanFactory中的内 ...
- Spring环境的搭建与测试 (spring2.5.6)
这里是采用的视频里面的spring版本 下载spring2.5.6, 然后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下 dist\spring.jar lib\jakarta-commo ...
- Ubuntu 通过Deb安装 MySQL5.5(转载)
1. 下载 MySQL 5.5 deb 安装包 cd /usr/local/src sudo wget -O mysql-5.5.22-debian6.0-i686.deb http://dev.my ...
- [转载] 深入 superviser
模块简介 新人接触线上的时候一般都会碰到supervise这个工具,导师对这个模块的解释一般就是,这个模块是监控进程,当进程挂掉之后,supervise会将进程启动.这样当进程出 现问题,如因出cor ...
- [转载] 每个 Python 程序员都要知道的日志实践
原文: http://python.jobbole.com/81666/ 在现实生活中,记录日志非常重要.银行转账时会有转账记录:飞机飞行过程中,会有黑盒子(飞行数据记录器)记录飞行过程中的一切.如果 ...
- python把元组组合成字典
list=((","16g"), (","32g"), (","red"), (","bl ...
- jpeg了解
JPEG是一个压缩标准,又可分为标准 JPEG.渐进式JPEG及JPEG2000三种: ①标准JPEG:以24位颜色存储单个光栅图像,是与平台无关的格式,支持最高级 别的压缩,不过,这种压缩是有损耗的 ...
- D3.js 弦图的制作
这是一种用于描述节点之间联系的图表. 1. 弦图是什么 弦图(Chord),主要用于表示两个节点之间的联系. 两点之间的连线,表示谁和谁具有联系: 线的粗细表示权重: 2. 数据 初始数据为: var ...