码迷,mamicode.com
首页 > 移动开发 > 详细

[LeetCode]Trapping Rain Water

时间:2015-04-20 09:23:52      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:leetcode

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!
题意:求由上面的数组组成的bar所组成的槽所能够容纳的水量,
思路1:首先找到最大的bar然后分别从左向右计算盛水量,计算时先选定一个非0的bar然后如果后面的bar不比当前大,则依次入栈,直到找到大于当前的,然后对站内的bar依次计算盛水量,最后从右向左重复上面的步骤。
代码1:

    public int trap1(List<Integer> height) {//两边向最长的bar扫面遇见比当前小的就入栈,直到遇到大的就弹出计算盛水量
        int sum = 0;

        int highIndex = 0;
        int max = Integer.MIN_VALUE;

        for(int i = 0; i < height.size(); ++ i){
            if(height.get(i) > max){
                max = height.get(i);
                highIndex = i;
            }
        }
        int left;
        Stack<Integer> s = new Stack<Integer>();
        for(int i = 0; i < highIndex;){
            while (i < highIndex && height.get(i) == 0){
                i ++;
            }
            left = height.get(i);
            while (i< highIndex) {
                while (i < highIndex && height.get(i) <= left) {
                    s.push(height.get(i));
                    i++;
                }
                while (!s.isEmpty()) {
                    int temp = s.peek();
                    s.pop();
                    sum += (left - temp);
                }
                left = height.get(i);
                i ++;
            }

        }
        int right = 0;
        for(int i = height.size() - 1; i > highIndex;){
            while (i > highIndex && height.get(i) == 0){
                i --;
            }
            right = height.get(i);
            while (i > highIndex){
                while (i > highIndex && height.get(i) <= right){
                    s.push(height.get(i));
                    i --;
                }
                while (!s.isEmpty()){
                    sum += right - s.peek();
                    s.pop();
                }
                right = height.get(i);
                i --;
            }
        }

        return sum;
    }
思路2:和上面的计算思路类似,这次不必要首先计算最大的bar,而是每次比较左右两边选定的bar的大小,小的那个先计算,依次循环。空间O(1),时间O(N)
代码:
    public int trap(List<Integer> height){
        int sum = 0;

        int left = 0, right = height.size() - 1;
        while (left < right && height.get(left) == 0){
            left ++;
        }
        while (right > left && height.get(right) == 0){
            right -- ;
        }
        int leftHeight,rightHeight;
        while (left < right) {
            leftHeight = height.get(left);
            rightHeight = height.get(right);
            if (leftHeight <= rightHeight ){//当左边bar高度小于右边的高度的时候,左边先扫描,直到左边遇到比leftHeight 高为止
                while (left < right && height.get(left) <= leftHeight){
                    sum += leftHeight - height.get(left);
                    left ++;
                }
            }else {
                while (left < right && height.get(right) <= rightHeight ) {
                    sum += rightHeight  - height.get(right);
                    right--;
                }
            }
        }
        return sum;
    }



[LeetCode]Trapping Rain Water

标签:leetcode

原文地址:http://blog.csdn.net/youmengjiuzhuiba/article/details/45146079

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!