`
hcx2013
  • 浏览: 82992 次
社区版块
存档分类
最新评论

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 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 Solution {
    public int trap(int[] height) {
        if (height == null || height.length == 0) {
        	return 0;
        }
        int res = 0;
        int[] left = new int[height.length];
        int[] right = new int[height.length];
        left[0] = height[0];
        int max = height[0];
        for (int i = 1; i < height.length; i++) {
        	 left[i] = Math.max(max, height[i]);
        	 max = Math.max(max, height[i]);
		}
        right[height.length-1] = height[height.length-1];
		max = height[height.length - 1];
		for (int i = height.length - 2; i >= 0; i--) {
			right[i] = Math.max(max, height[i]);
			max = Math.max(max, height[i]);
		}
		for (int i = 1; i < height.length-1; i++) {
			int area = Math.min(left[i], right[i]) - height[i];
			if (area > 0) {
				res += area;
			}
		}
       return res;
    }
}

 

5
2
分享到:
评论

相关推荐

    leetcode1004-leetcode:leetcode删号重练个人记录:)

    Trapping Rain Water 刷题顺序 刷题内容总的来说包括数据结构、算法和技巧 按照tag分类别进行刷题,跳过like&lt;200&gt;like的题目 按Acceptance由高到低进行,每个tag所刷题目大于20(对于类型较少的题目大于5),则进入下...

    javalruleetcode-LeetCode:LeetCode算法问题

    Trapping Rain Water LeetCode 61 RotateList LeetCode 75 Sort Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels...

    lrucacheleetcode-leetcode:leetcode

    Trapping Rain Water 85. Maximal Rectangle Monotonic stack for 2d array 239. Sliding Window Maximum 255. Verify Preorder Sequence in Binary Search Tree 907. Sum of Subarray Minimums 二叉搜索树 99. ...

    cpp-算法精粹

    Trapping Rain Water Rotate Image Plus One Climbing Stairs Set Matrix Zeroes Gas Station Candy Majority Element Rotate Array Contains Duplicate Contains Duplicate II Contains Duplicate III Product of ...

    leetcode卡-LeetCode:我的LeetCode解决方案

    Trapping Rain Water II], BFS/Priority queue 2017.06.19 打卡[LeetCode 145. Binary Tree Postorder Traversal], Tree/stack 2017.06.20 打卡[LeetCode 107. Binary Tree Level Order Traversal II], Tree/BFS ...

    Leetcode-Solutions:JavaScript 语言的 Leetcode 解决方案

    力码解决方案 Leetcode是一个网站,人们——...├── Trapping Rain Water │ ├── Readme.md │ └── solution.js ├── Wildcard Matching │ ├── Readme.md │ └── solution.js ├── Valid Number │

    leetcode添加元素使和等于-leetcode:我的leetcode解决方案

    Trapping Rain Water Integer to English Words Regular Expression Matching Merge K Sorted Lists Remove Invalid Parentheses Serialize and Deserialize Binary Tree Minimum Window Substring C++ STL中常见...

    LeetCode最全代码

    ...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...

    leetcode答案-Algorithms:算法

    二、trapping rain water 雨水收集问题 问题描述 给定一个 number 组成的数组,值代表箱子的高度,下雨后,这些箱子中的间隙一共能收集到多少雨水 解答 暴力破解: 遍历数组,找当前 index 往左的最大值和当前 index...

    多线程leetcode-leetcode-java:leetcode上的题解,基于java语言

    多线程 leetcode 前言 每天刷点leetcode,基于java语言实现。 leetcode上难度分三档:easy,medium,hard. 如下: easy medium Remove Nth Node From End of List Swap Nodes ...Trapping Rain Water

    leetcode不会-3D-Trapping-Rain-Water-System:该repo包含复杂3D雨水收集系统的解决方案,该系统在Lee

    leetcode 不会3D 捕集雨水系统 该 repo 包含复杂 3D 雨水收集系统的解决方案,该系统在 Leetcode Problem ...您必须编写一个函数来计算如果我们将无限供应的水倒在该建筑上时将存储在该建筑中的水量。...

    erlang入门级练习:LeetCode OJ问题的部分erlang 源码

    我自己在新学erlang,在LeetCode OJ上找了题目练习,题目很适合新手... "three_sum.erl","trapping_rain_water.erl", "valid_palindrome.erl" 个人认为dungeon_game这个题目解题逻辑很体现erlang的函数式的思维逻辑

    收集面试中提出的一些重要问题。-C/C++开发

    收集面试中提出的一些重要问题。 数据结构算法面试问题面试中提出的一些重要问题的集合。...数组https://leetcode.com/problems/3sum/ [解决方案] https://leetcode.com/problems/trapping-rain-water/ [解决方案] ...

    algorithm-homework

    作业说明 代码注释中有力扣链接. 第一周作业 循环队列, 双向循环队列, 双向队列, 双向链表, 链表, 爬楼梯, 盛最多水的容器, 柱状图中最大的矩形, ...删除有序数组中的重复项, ...接雨水, trapping-rain-water.ts 两数之和,

    leetcode跳跃-leetcode:leetcode解题之路

    接雨水](./Array/trapping-rain-water.md) [0048 旋转图像](./Array/rotate-image.md) Heap 堆 [0023 合并K个排序链表](./Heap/merge-k-sorted-lists.md) String 字符串 [0006 Z字形变换](./String/zigzag-...

    leetcode答案-leetcode:leetcode.com问题的答案

    leetcode 答案leetcode leetcode.com 问题的答案 跑步 python -m venv .venv source .venv/bin/activate pip install -r requirements.txt pytest &lt;question&gt;/tests.py ...lc42_trapping_rain_water/tests.py

    leetcode添加元素使和等于-leetcode:力码

    trapping-rain-water 滑动窗口 通过窗口的大小不断调整来找到合适的值,或者窗口大小不变,通过左右移动来找到相应的结果 \ \ 其他 非 LeetCode 题,单纯在别人面试中看到的 \ 链表 \ swap-nodes-in-pairs linked-...

Global site tag (gtag.js) - Google Analytics