这是一个非常有意思的问题,求解最大容积问题,值得动脑筋想一想。

原题例如以下:

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

For example, 

Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing
this image!

图中浅颜色的就是雨水的含量,从图中我们不难发现,在每一列有水的雨水含量就是这一列左右两側最高的两个柱子中的较矮的柱子和当前列的高度差。这样我们就能逐次计算出来分列的含量。当然,我们能够在计算每一列的时候都计算一遍两側的最大值,可是这样是浪费时间的,我们能够分治解决,先求解出最高的柱子,这样这一柱子在一定时刻内能够当做当中一側的最值,逐渐缩小范围就可以。以下是自己写的代码,可能比較乱,可是思想还是能够感受一下的,在最后会贴出来大牛们写的高质量代码。

<span style="font-size:18px;">class Solution {
public:
int max_value(int a,int b)
{
return a>b?a:b;
}
int max_element_position(int A[],int begin,int end)
{
int max = A[begin];
int pos = begin;
for(int i = begin+1;i<=end;i++)
if(A[i]>max)
{
max = A[i];
pos = i;
}
return pos;
}
int water(int A[],int start,int left_max,int end,int right_max)
{
int left = 0,right = 0,mid = 0;
if(start == end)
{
if(left_max>A[start] && right_max>A[start])
return min(left_max,right_max) - A[start];
else
return 0;
}
int max = max_element_position(A,start,end);
if(A[max]<left_max && A[max]<right_max)//这里要注意,中间的最大值也可能上面存储水
{
mid = min(left_max,right_max) - A[max];
} if(max-1 >= start)
left = water(A,start,left_max,max-1,max_value(right_max,A[max]));
if(max+1 <= end)
right = water(A,max+1,max_value(left_max,A[max]),end,right_max);
return left+right+mid;
}
int trap(int A[], int n) {
if(n<=2)
return 0;
return water(A,0,0,n-1,0);
}
};</span>

以下的方法确实是非常理解了非常多,不错不错。

int trap(int A[], int n) {
// Note: The Solution object is instantiated only once.
if(A==NULL || n<1)return 0; int maxheight = 0;
vector<int> leftMostHeight(n);
for(int i =0; i<n;i++)
{
leftMostHeight[i]=maxheight;
maxheight = maxheight > A[i] ? maxheight : A[i];
} maxheight = 0;
vector<int> rightMostHeight(n);
for(int i =n-1;i>=0;i--)
{
rightMostHeight[i] = maxheight;
maxheight = maxheight > A[i] ? maxheight : A[i];
} int water = 0;
for(int i =0; i < n; i++)
{
int high = min(leftMostHeight[i],rightMostHeight[i])-A[i];
if(high>0)
water += high;
}
return water;
}

每日算法之三十三:Trapping Rain Water的更多相关文章

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

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

  2. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

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

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

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

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

  5. [LeetCode] 42. Trapping Rain Water 收集雨水

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

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

  7. [LintCode] Trapping Rain Water 收集雨水

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

  8. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

  9. 有意思的数学题:Trapping Rain Water

    LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...

随机推荐

  1. 让AllocateHwnd接受一般函数地址作参数(105篇博客)

    http://www.xuebuyuan.com/1889769.html Classes单元的AllocateHWnd函数是需要传入一个处理消息的类的方法的作为参数的,原型: function Al ...

  2. Lucene.Net 2.3.1开发介绍 —— 二、分词(三)

    原文:Lucene.Net 2.3.1开发介绍 -- 二.分词(三) 1.3 分词器结构 1.3.1 分词器整体结构 从1.2节的分析,终于做到了管中窥豹,现在在Lucene.Net项目中添加一个类关 ...

  3. 网络数据(socket)传输总结

    环境限定:TCP/IP下的socket网络传输:C/C++开发语言,32/64位机. 目前有两种方式对数据进行传输:1)字符流形式,即将数据用字符串表示:2)结构型方式,即将数据按类型直接传输. 1) ...

  4. rcp(插件开发)org.eclipse.ui.decorators 使用

    org.eclipse.ui.decorators这个扩展点可以为对应的节点添加不同的图标显示. 使用方式都差不多,以下就转载一下使用方式: 1.添加扩展点 org.eclipse.ui.decora ...

  5. spring中bean的设计模式

    默认的是单例的. 如果不想单例需要如下配置: <bean id="user" class="..." singleton="false" ...

  6. oracle数据库单个数据文件的大小限制

    之前没有仔细想过这个问题,因为总是不会用到,也没有犯过类似错误. 顺便提一下学习方法吧. 卤肉的学习方法是:常用知识点,熟悉理论并反复做实验,深入理解:不常用的知识点,相关内容都了解大概,遇到问题时想 ...

  7. [半原创]指纹识别+谷歌图片识别技术之C++代码

    原地址:http://blog.csdn.net/guoming0000/article/details/8138223 以前看到一个http://topic.csdn.net/u/20120417/ ...

  8. 解析php混淆加密解密的手段,如 phpjm,phpdp神盾,php威盾

    原文 解析php混淆加密解密的手段,如 phpjm,phpdp神盾,php威盾 php做为一门当下非常流行的web语言,常常看到有人求解密php文件,想当年的asp也是一样.一些人不理解为什么要混淆( ...

  9. Android开发周报:Flyme OS开源、经典开源项目解析

    Android开发周报:Flyme OS开源.经典开源项目解析 新闻 <魅族Flyme OS源码上线Github> :近日魅族正式发布了MX5,并且在发布会上,魅族还宣布Flyme OS开 ...

  10. hdu4059 The Boss on Mars

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...