42.接雨水 Trapping Rain Water


本人的解法:

public class Main {
    public int trap(int[] height) {
        // 统计雨水总数//temp登记最大值
        int sum = 0;
        if (height.length != 0)
        {
            int temp = height[0];
            int temp2 = 0;
            int temp3 = height[0];
            int temp4 = height[height.length - 1];
            //跟踪最大值的位置
            for (int x = 0; x < height.length; x++)
            {
                if (height[x] > temp) {
                    temp = height[x];
                    temp2 = x;
                }
            }
            // 进行累加 这里采用两头向中间逐级判断
            //第一个for
            for (int i = 1; i <= temp2; i++)
            {

                if (height[i] > temp3)
                {
                    temp3 = height[i];
                }
                else
                {
                    sum = sum + temp3 - height[i];
                }
            }
            //第二个for
            for (int i = height.length - 1; i >= temp2; i--)
            {
                if (height[i] > temp4)
                {
                    temp4 = height[i];
                }
                else
                {
                    sum = sum + temp4 - height[i];
                }
            }
            return sum;
        }
            //空集合
        else
        {
            return 0;
        }
    }
    //测试代码
    public static void main(String[] args) {
        Main m = new Main();
        int[] arr = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 };
        System.out.println(m.trap(arr));
    }
}
//////////////////////////////简化版
public class Solution {
    public int trap(int[] A) {
        int left = 0 , right = A.length-1;
        int sum = 0;
        int pre = 0;
        while(left < right){
            sum += (Math.min(A[left], A[right])-pre) * (right-left-1);
            pre = Math.min(A[left],A[right]);
            if(A[left] > A[right]){
                int temp = right-1;
                while(left < temp && A[temp] <= pre){sum-=A[temp];temp--;}
                if(left < temp) sum -= pre;
                right = temp;
            }else{
                int temp = left+1;
                while(temp < right && A[temp] <= pre){sum-=A[temp];temp++;}
                if(temp < right) sum -= pre;
                left = temp;
            }
        }
        return sum;
    }
}
?


最大值的位置只选一个(如果几个最大值一样的话 也只选其中一个)
其他解法:
1、填充集合为凸集 填充的单位

2、

class Solution {
    public int trap(int[] height) {
        if(height.length<3){
            return 0;
        }
        return find(height,0,height.length-1);
    }
    public int find(int[] height,int start,int end){
        if(end-start<2){//递归的终点
            return 0;
        }
        int max=-1,tmp=-1,min_two=Math.min(height[start],height[end]),sum=0;;
        for(int i=start+1;i<end;i++){
            //这一句写在哪里都行
            sum=sum+(min_two-height[i]);
            if(height[i]>max){
                max=height[i];
                tmp=i;
            }
        }
        if(max<min_two){//上面的加法其实应该在这里,转移到上面和在这里其实都一样
            //sum=sum+(min_two-height[i]);
            return sum;
        }else{//其实这里还可以优化一下当中间的max值等于start或者end的时候,当它等于start,那么直接计算即可,不用进行下一次递归,因为下一次递归会再扫描一遍
            return find(height,start,tmp)+find(height,tmp,end);
        }
    }
}
public int trap(int[] A) {
        if (A==null) return 0;
        Stack<Integer> s = new Stack<Integer>();
        int i = 0, maxWater = 0, maxBotWater = 0;
        while (i < A.length){
            if (s.isEmpty() || A[i]<=A[s.peek()]){
                s.push(i++);
            }
            else {
                int bot = s.pop();
                maxBotWater = s.isEmpty()? // empty means no il
                0:(Math.min(A[s.peek()],A[i])-A[bot])*(i-s.peek()-1);
                maxWater += maxBotWater;
            }
        }
        return maxWater;
    }

原文地址:https://www.cnblogs.com/cznczai/p/11149678.html

时间: 2024-10-10 02:50:25

42.接雨水 Trapping Rain Water的相关文章

leetCode 42.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

No.42 Trapping Rain Water

1 class Solution 2 { 3 public: 4 int trap(vector<int> &height) 5 {/* 6 雨水覆盖问题: 7 每一个bar能承受的水量为:它左侧所有中最高的和右侧所有中最高的中取最小值作为一个瓶颈[否则也留不住], 8 若该值大于当前bar的高度,其差值即为所求 9 累加所有bar能承受的水量即为所求 10 法三:[左右两个指针] 11 找到最高的位置,将数组分为两部分: 12 对左侧数据,因为右边已经有了最高值,所以,类似法一,找到

刷题42. Trapping Rain Water

一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的解法 这个题目是找"坑",然后计算里面可以存的"雨".总共提交了5次,前面几次都是边界错误. 代码如下: #include<iostream> #include<vector> using namespace std; class Solutio

42. Trapping Rain Water(js)

42. 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. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1].

11 Container With Most Water 42.Trapping Rain Water

11 和 42 本质上都是木桶原理: 11 如何才能存最多的水? 假设 a[left] < a[right] ,    total = a[left] *(right-left) ,  那么 right -1, right-2 位置 都比 total 小, 此时就没必要move right 了, 因为所有的right -x 都比 right 小. 此时只需要move left 指针, 继续找最大value. code 很简单: public int maxArea(int[] height) {

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

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