接雨水

问题描述

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]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

Example:

Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

解题思路

解法一

根据题意和实例,我第一个想到的解决方法是通过栈的方式实现,先找到这个数组的最大值的下标。从下标0遍历到最大值下标,通过栈操作计算出前半段的积水。从最大下标到n-1, 和前半段计算方式相同。

public int trap(int[] height) {
    	if(height.length == 0 || height.length == 1 || height.length == 2) {
    		return 0;
    	}
    	int result = 0;
    	
    	// 寻找最大值下标
    	int index = 0, max = height[0];
    	for(int i = 1; i < height.length; i++ ) {
    		if(height[i] > max) {
    			index = i;
    			max = height[i];
    		}
    	}
    	Stack<Integer> stack = new Stack<Integer>();
    	// 除去数组开头连续为0的项
    	int n = 0 ;
        while(height[n] == 0){
            n++;
        }
        stack.push(height[n]);
        int left = height[n];
        int start = n; 
    	for(int i = n + 1; i <= index; i++ ) {
    		
    		if(height[i] < left) {
    			stack.push(height[i]);
    		}else if(height[i] >= left){
    			int s = 0; 
    			while(!stack.empty()) {
    				s += stack.pop();
    			}
    			// 计算积水
    			result += (i - start) * left - s;
    			stack.push(height[i]);
    			left = height[i];
    			start = i;
    		}
    		
    	}
    	
    	// 计算后半段
    	stack.clear();
    	// 除去数组末尾连续为0
    	int m = height.length - 1;
    	while(height[m] == 0){
            m--;
        }
    	stack.push(height[m]);
        left = height[m];
        start = m;
        for(int j = m - 1; j >= index; j--) {

    		if(height[j] < left) {
    			stack.push(height[j]);
    		}else if(height[j] >= left){
    			int s = 0; 
    			while(!stack.empty()) {
    				s += stack.pop();
    			}
    			// 计算积水
    			result += (start - j) * left - s;
    			stack.push(height[j]);
    			left = height[j];
    			start = j;
    		}
    		
    		
    	}
        
        return result;
        
    }

在LeetCode执行为4ms

解法二

第二种方法不太容易理解,但是解题思想很巧妙,通过双指针逼夹的方式。结合上面图比较容易理解。

public int trap(int[] A) {
        int a=0;
        int b=A.length-1;
        int max=0;
        int leftmax=0;
        int rightmax=0;
        while(a<=b){
            leftmax=Math.max(leftmax,A[a]);
            rightmax=Math.max(rightmax,A[b]);
            if(leftmax<rightmax){
                max+=(leftmax-A[a]);
                a++;
            }
            else{
                max+=(rightmax-A[b]);
                b--;
            }
        }
        return max;
    }

在LeetCode执行时间为2ms

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值