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

原题例如以下:

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. css Gradients(渐变)

    渐变分为4类 1:线性渐变(Linear Gradients)- 向下/向上/向左/向右/对角方向 2:径向渐变(Radial Gradients)- 由它们的中心定义 3:对角渐变 4:角度渐变 以 ...

  2. cocos2d-x学习日志(10) --射击游戏(喵星战争)

    转载请标明:转载自[小枫栏目],博文链接:http://blog.csdn.net/rexuefengye/article/details/10553487 一.纵版射击游戏的特点 纵版射击游戏是一种 ...

  3. Linux学习记录--匿名沟通渠道

    匿名沟通渠道 管道Linux最初支持Unix IPC其中的一种形式.具有下列特征: 1.管道是半双工.数据可以仅在一个方向流动:当双方需要沟通.建设两条管线需要. 2.仅仅能用于父子进程或者兄弟进程之 ...

  4. 【deep learning学习笔记】注释yusugomori的LR代码 --- LogisticRegression.cpp

    模型实现代码,关键是train函数和predict函数,都很容易. #include <iostream> #include <string> #include <mat ...

  5. 深入探讨MFC消息循环和消息泵

    首先,应该清楚MFC的消息循环(::GetMessage,::PeekMessage),消息泵(CWinThread::PumpMessage)和MFC的消息在窗口之间的路由是两件不同的事情.在MFC ...

  6. CURD演示 2

    <?php class UserAction extends Action{ public function index(){ echo "你好!"; $m=M('user' ...

  7. C++ operator overload -- 操作符重载

    C++ operator overload -- 操作符重载 2011-12-13 14:18:29 分类: C/C++ 操作符重载有两种方式,一是以成员函数方式重载,另一种是全局函数. 先看例子 # ...

  8. SQL查询数据封装JavaBean对象

    public static List getListBySql(String sql, Class cls){   List list = new ArrayList();   Connection ...

  9. Delphi使用StrToDatetime在不同操作系统出现不同的情况(控制面板的时间格式都记录在注册表里,因此也可修改注册表)

    Str:=  '2010-4-13  06:22:22'; StrToDateTime(Str); 现象:在WinXP, Win2003 都不会报错 但是在Windows7,Windows Serve ...

  10. spice for openstack

    nova.conf vnc_enabled=False [Spice] agent_enabled=True enabled=True html5proxy_base_url=http://x.x.x ...