LintCode-接雨水

给出 n 个非负整数,代表一张X轴上每个区域宽度为 1 的海拔图,
计算这个海拔图最多能接住多少(面积)雨水。

样例

如上图所示,海拔分别为 [0,1,0,2,1,0,1,3,2,1,2,1],
返回 6.

挑战

O(n) 时间, O(1) 空间

O(n) 时间, O(n) 空间也可以接受

分析:只能遍历一遍,且不能存储所有的高度,一块直柱能接的水取决于左右两边较短的高度,所以一个比较直观的方法是从左到有遍历一遍记录该点左边的最大值,从右到左遍历一遍记录该点的右边的最大值,此方法为O(n)
时间, O(n) 空间,第二种方法可以利用单调递减栈来实现,此方法为O(n) 时间, O(n) 空间,第三种方法为先找出中间最大点,然后左边部分相当于已经确定了右边最大值,左指针不断向右逼近就行,右边部分相当于记录了左边最大值,右指针不断向左逼近就行,此方法为O(n)
时间, O(1) 空间,第四种方法类似第三种,左右夹逼,每次选择小的那个指针向中间靠拢,此方法为O(n) 时间, O(1) 空间,但是要比方法三少遍历一遍。

我们在这里实现第四种最优的方案:

代码:

class Solution {
public:
    /**
     * @param heights: a vector of integers
     * @return: a integer
     */
    int trapRainWater(vector<int> &heights) {
        // write your code here
        int ret = 0;
        if(heights.size()==0)
            return ret;
        int lmax = 0, rmax = 0;
        int l = 0, r = heights.size()-1;
        while(l<r)
        {
            lmax = max(lmax,heights[l]);
            rmax = max(rmax,heights[r]);
            if(lmax<rmax)
                ret += lmax-heights[l],l++;
            else
                ret += rmax-heights[r],r--;
        }
        return ret;
    }
};
时间: 2024-07-31 09:23:34

LintCode-接雨水的相关文章

[LintCode] 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. Have you met this question in a real interview? Yes Example Given [0,1,0,2,1,0,1,3,2,1,2,1], return

[lintcode the-smallest-difference]最小差(python)

题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回最小差. 排好序后用两个指针分别扫描两个数组,每次更新他们的差值的绝对值.并且依据他们两个数字的大小来决定谁来移动指针. 1 class Solution: 2 # @param

lintcode.44 最小子数组

最小子数组 描述 笔记 数据 评测 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 注意事项 子数组最少包含一个数字 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Airbnb Amazon LinkedIn Cryptic Studios Dropbox Apple Epic Systems TinyCo Yelp Hedvig Zenefits Uber Snapchat Yahoo Microsoft Bloomberg Facebook Google

code第一部分:数组 第十四题 雨水问题

code第一部分:数组 第十四题 雨水问题 Given n non-negative integers representing an elevation map where the width of each bar is 1, computehow 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. 分析: 解决方案1 对于每个柱子,找到其左右两

lintcode 66.67.68 二叉树遍历(前序、中序、后序)

AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The r

[LintCode/LeetCode]——两数和、三数和、四数和

LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号:56,链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 题目描述: 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 1

Lintcode 469. 等价二叉树

----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */

Lintcode 75.寻找峰值

--------------------------------------- 按照给定的峰值定义,峰值的左半部分一定是递增的,所以只要找到不递增的即可. AC代码: class Solution { /** * @param A: An integers array. * @return: return any of peek positions. */ public int findPeak(int[] A) { for(int i=1;i<A.length;i++){ if(A[i]>=

Lintcode 9.Fizz Buzz 问题

------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of strings. */ public ArrayList<String> fizzBuzz(int n) { ArrayList<String> results = new ArrayList<String>(); for (int i = 1; i <= n; i++

Lintcode 97.二叉树的最大深度

--------------------------------- AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class S