每日算法之三十三:Trapping Rain Water

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

原题如下:

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

时间: 2024-12-16 23:26:31

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

Twitter算法面试题详解(Java实现)——Trapping Rain Water

[算法题:求出高低不同的砖中能存多少水] 「Trapping Rain Water」 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.

有意思的数学题:Trapping Rain Water

LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的部分 假设:积木宽度均为1 输入:各个积木的高度 输出:所有积木能容纳水的“面积” 思考过程 1. 逐一求积木的间隔似乎不太容易.特别对于图中3-7积木间的容积,如果可以先求底部(4-6间)的容积,当求解上层(3-7)的容积时,还需要做额外的处理,如减掉底部的高度. 2. 既然如此,可否先求出3-7

[LeetCode][JavaScript]Trapping Rain Water

Trapping Rain Water 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

[LintCode] Trapping Rain Water II

Trapping Rain Water II Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining. Example Given 5*4 matrix [12,13,0,12] [13,4,13,12] [13,8,10,12] [

每日算法之四十三:Rotate List (列表旋转k个元素)

Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 这里的k可能是比链表长度要大的数字,因此实际旋转的位置就是k%len(list).如果这个计算结果等于零或者等于len(list),

每日算法之三十七:Rotate Image (图像旋转)

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 原地图像顺时针旋转90度.因为要求空间复杂度是常数,因此应该迭代旋转操作. class Solution { public: void rotate(vector<vector<int> > &mat

Leetcode 407. Trapping Rain Water II

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note:Both m and n are less than 110. The height of each unit cell is greater tha

Trapping Rain water 诸多细节须注意

Trapping Rain Water Total Accepted: 35650 Total Submissions: 118319My Submissions Question Solution 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.

LeetCode 042 Trapping Rain Water

题目要求:Trapping Rain Water 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 elevatio