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

原题例如以下:

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. Xamarin 后台持续定位与提示

    IOS后台持续运行对于c#程序员不懂得ios后台机制的是存在一定困扰的.特别是ios9过后对后台和安全进行了更严格的限制 好了废话不多说 一 设置info.plist权限信息 参考: 后台模式:htt ...

  2. 循环获取json对象的属性名

    今天做项目遇到一个难题,asp.net 项目,数据库中一个表有八十多个字段,我已经在前台将表转化为了json字符数组,我要在前台循环这八十多个字段,我只能根据属性名来处理,一筹莫展,最终解决,收益颇多 ...

  3. SilkTest Q&A 2

    Q11:SilkTest中有没有计算web页面上单词数量的函数? A11:你可以使用Clipboard函数.使用Ctrl+a和Ctrl+c,然后解析string的list. Q12:silktest的 ...

  4. 如何制作python安装模块(setup.py)

    Python模块的安装方法: 1. 单文件模块:直接把文件拷贝到$python_dir/lib 2. 多文件模块,带setup.py:python setup.py install 3. egg文件, ...

  5. 【图像处理】Bilinear Image Scaling

    Bilinear image scaling is about the same as nearest neighbor image scaling except with interpolation ...

  6. OpenRisc-50-or1200的freeze模块分析

    引言 之前,我们分析or1200的控制通路中的sprs模块和except模块,本小节,我们就分析一下or1200控制通路的最后一个模块,就是freeze模块. 1,整体分析 freeze模块,顾名思义 ...

  7. hdu2066一个人的旅行(dijkstra)

    Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰 ...

  8. Extjs4.2 Desktop 拖动黑色和白色的桌面图标的解决方案

    最近做了一个extjs4.2的desktop桌面demo,该desktop从原来的包中剥离出来,并实现了桌面图标休息,拖动桌面图标,但是,用户抱怨拖动桌面图标会出现黑色和白色,测试,在 extjs4. ...

  9. kobox : key_wq.c -v1 如何使用工作队列 workqueue

    kobox: key_wq.c - v1 说明: TQ2440主要驱动因素,四个按键驱动的处理 key_wq.c和key.c类别似,与key.c之间的差异的主要驱动力: key.c使用计时器,在中断发 ...

  10. VSTO学习笔记(一)VSTO概述

    原文:VSTO学习笔记(一)VSTO概述 接触VSTO纯属偶然,前段时间因为忙于一个项目,在客户端Excel中制作一个插件,从远程服务器端(SharePoint Excel Services)上下载E ...