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]的更多相关文章

  1. Trapping Rain Water leetcode java

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  2. [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 ...

  3. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  4. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

  5. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  6. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  7. LeetCode: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

  8. [LeetCode] 接雨水,题 Trapping Rain Water

    这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...

  9. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

随机推荐

  1. FZU 2216 The Longest Straight(最长直道)

    Description 题目描述 ZB is playing a card game where the goal is to make straights. Each card in the dec ...

  2. [资源] Getting started with Tensorflow

    Learning Website / Blog: Official Tutorial: https://www.tensorflow.org/tutorial Learn Tensorflow: ht ...

  3. Python基础学习笔记(四)语句

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...

  4. HDU 5422 Rikka with Graph

    Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. 初识Oracle

    登录时无法登陆:   无监听程序 ———>启动服务OracleXETNListener   监听程序当前无法识别连接描述中的请求服务————>重启服务       OracleServic ...

  6. iOS - UIWebView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIWebView : UIView <NSCoding, UIScrol ...

  7. spring事务知识

    事务的传播行为? 在Spring 的事务中, _可以通过 propagation 来定义事务的传播行为_: PROPAGATION_required:如果当前没有事务,就新建一个事务,如果已经存在一个 ...

  8. 简单的php性能注意点

    什么情况,可能遇到性能问题: 1.php语法使用的不恰当 2.使用php语言做了它不擅长做的事 3.用php语言连接的服务不给力 4.php自身的短板 5.我也不知道的问题 一般情况:php性能问题不 ...

  9. hdu 2063 过山车(二分图最佳匹配)

    经典的二分图最大匹配问题,因为匈牙利算法我还没有认真去看过,想先试试下网络流的做法,即对所有女生增加一个超级源,对所有男生增加一个超级汇,然后按照题意的匹配由女生向男生连一条边,跑一个最大流就是答案( ...

  10. linux 相关学习记录

    (一)概念① 物理CPU实际Server中插槽上的CPU个数物理cpu数量,可以数不重复的 physical id 有几个② 逻辑CPU /proc/cpuinfo 用来存储cpu硬件信息的信息内容分 ...