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

原题例如以下:

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. Lucene.Net 2.3.1开发介绍 —— 简介

    原文:Lucene.Net 2.3.1开发介绍 -- 简介 Lucene.Net是Lucene在dot net平台上的移植版本.它的功能与Lucene一样,都是用来提供一组API,让我们能快速开发自己 ...

  2. mojo 关闭utf8

    [root@wx03 ~]# cat test.pl use Mojolicious::Lite; use JSON qw/encode_json decode_json/; use Encode; ...

  3. form表单和表格

    HTML <table> 标签 border pixels 规定表格边框的宽度. STF cellpadding pixels % 规定单元边沿与其内容之间的空白. STF cellspa ...

  4. HDU--杭电--4504--威威猫系列故事——篮球梦--DP

    威威猫系列故事——篮球梦 Time Limit: 300/100 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total ...

  5. STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())

    一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search  -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...

  6. Face-landmarks-detection-benchmark 人脸特征定位网站汇总

    源地址:https://www.douban.com/note/525032729/   https://github.com/delphifirst/FaceXhttps://github.com/ ...

  7. jquery怎么在点击li标签之后添加一个在class,点击下一个li时删除上一个class?

    思路:点击当前li元素后是用removeClass()删除所有兄弟元素(使用siblings()获取)的class样式,然后使用addClass()为当前li添加class. 具体演示如下: 1.HT ...

  8. Mojo 分析日志接口

    #!/usr/bin/perl #取文件行数 ##循环开始清空文件 use POSIX; use DBI; my $dir = '/data01/applog_backup'; my $file = ...

  9. 随机IP代理

    第一个例子就设置了一个代理IP,也是不靠谱的,最好的方式就是多设置几个,如第二个例子,通过http://www.youdaili.net/Daili/你可以找到很多代理IP, 抓取国内网站时尽量选取中 ...

  10. Dnasp计算LD

    Dnasp计算LD Table of Contents 1 Dnasp 计算LD 1 Dnasp 计算LD Dnasp有很多的功能,现在主要来记录其计算LD的功能. 首先File——然后打开data— ...