• leetcode — trapping-rain-water


    /**
     * Source : https://oj.leetcode.com/problems/trapping-rain-water/
     *
     * Created by lverpeng on 2017/7/15.
     *
     * 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.
     *
     *     ^
     *     |
     *   3 |                       +--+
     *     |                       |  |
     *   2 |          +--+xxxxxxxxx|  +--+xx+--+
     *     |          |  |xxxxxxxxx|  |  |xx|  |
     *   1 |   +--+xxx|  +--+xxx+--+  |  +--+  +--+
     *     |   |  |xxx|  |  |xxx|  |  |  |  |  |  |
     *   0 +---+--+---+--+--+---+--+--+--+--+--+--+----->
     *       0  1   0  2  1   0  1  3  2  1  2  1
     *
     * 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!
     *
     *
     *
     */
    public class TrappingRainWater {
    
    
        /**
         * 找到数组组成的柱条之间存水的单位数
         *
         * 先找到最大的,分为左右两边
         * 依次遍历左边,将当前值与左边最大的值比较,如果大于左边最大值则更新左边最大值,计算该位置可以存的水量
         * 右边类似
         *
         *
         * @param num
         */
        public int trap (int[] num) {
            int max = 0;
            int maxIndex = 0;
            for (int i = 0; i < num.length; i++) {
                if (max < num[i]) {
                    max = num[i];
                    maxIndex = i;
                }
            }
    
            // 当前位置以前的较大值
            int preMax = 0;
            int result = 0;
            // 计算左边
            for (int i = 0; i < maxIndex; i++) {
                if (preMax < num[i]) {
                    preMax = num[i];
                }
                result += preMax - num[i];
            }
    
            // 计算右边
            int afterMax = 0;
            for (int i = num.length - 1; i > maxIndex ; i--) {
                if (afterMax < num[i]) {
                    afterMax = num[i];
                }
                result += afterMax - num[i];
            }
    
            return result;
        }
    
        public static void main(String[] args) {
            TrappingRainWater trappingRainWater = new TrappingRainWater();
            int[] arr = new int[]{0,1,0,2,1,0,1,3,2,1,2,1};
            System.out.println(trappingRainWater.trap(arr));
        }
    }
    
  • 相关阅读:
    js数组元素的添加和删除
    jquery中prop()方法和attr()方法的区别浅析
    jquery选择器 之 获取父级元素、同级元素、子元素
    jQuery 序列化表单数据 serialize() serializeArray()
    Awesome Python,Python的框架集合
    关于树莓派HDMI转VGA线接显示器黑屏
    python爬虫xpath的语法
    爬虫下载百度贴吧图片
    项目经理的磨练(2) 科学的安排项目日程安排
    项目经理的磨练
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7472063.html
Copyright © 2020-2023  润新知