TrappingRainWater

问题描述:

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.

算法分析:观察下就可以发现被水填满后的形状是先升后降的塔形,因此,先遍历一遍找到塔顶,然后分别从两边开始,往塔顶所在位置遍历,水位只会增高不会减小,且一直和最近遇到的最大高度持平,这样知道了实时水位,就可以边遍历边计算面积。

public class TrapingRainWater
{
	public int trap(int[] height)
	{
		int n = height.length;
		if(n <= 2)
		{
			return 0;
		}
		int max = -1;
		int maxIndex = 0;
		for(int i = 0; i < n; i ++)
		{
			if(height[i] > max)
			{
				max = height[i];
				maxIndex = i;
			}
		}
		int area = 0;
		int root = height[0];
		for(int i = 0; i < maxIndex; i ++)
		{
			if(root < height[i])
			{
				root = height[i];
			}
			else
			{
				area += (root - height[i]);
			}
		}
		root = height[n-1];
		for(int i = n-1; i > maxIndex; i --)
		{
			if(root < height[i])
			{
				root = height[i];
			}
			else
			{
				area += (root - height[i]);
			}
		}
		return area;
	}

}
时间: 2024-09-30 05:06:19

TrappingRainWater的相关文章

有意思的数学题: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

LeetCode——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 a

[LeetCode]题解(python):042-Trapping Rain Water

题目来源: https://leetcode.com/problems/trapping-rain-water/ 题意分析: 输入一组数组,代表一个宽度为1的高度地图.问,这个地图在雨后可以收集多少水.例如,输入一个数组[0,1,0,2,1,0,1,3,2,1,2,1],返回的是6.如图所示: 题目思路: 这道题目虽然说是hard难度的题目,但是其实不是很难.不难发现,水都是从最高那个数起和第二高数之间.那么这题可以分成两部.①找到数组的最大值.②计算最大值左边和右边分别可以收集多少水:计算左边

[leetcode]Trapping Rain Water @ Python

原题地址:https://oj.leetcode.com/problems/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,

[Leetcode][Python]42: Trapping Rain Water

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 42: Trapping Rain Waterhttps://oj.leetcode.com/problems/trapping-rain-water/ Given n non-negative integers representing an elevation map where the width of each bar is 1,compute how much water

收集雨水问题

直方图收集雨水问题: 给定n个非负整数,表示直方图的方柱的高度,同时,每个方柱的宽度假定都为1,若这样形状的容器收集雨水,可以盛多少雨水. 如:输入0,1,0,2,1,0,1,3,2,1,2,1:返回为6. 如图所示: 程序实现: 1 #include <iostream> 2 using namespace std; 3 4 int TrappingRainWater(int A[],int n){ 5 int SecHight = 0;//当前找到的第二大数 6 int left = 0;

LeetCode题目总结分类

注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心)http://oj.leetcode.com/problems/valid-parentheses/http://oj.leetcode.com/probl

转载 ACM训练计划

leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心)http://oj.leetcode.com/problems/valid-parentheses/http://oj.leetcode.com/problems/largest-rectang