资讯专栏INFORMATION COLUMN

[LintCode/LeetCode] Trapping Rain Water [栈和双指针]

bluesky / 953人阅读

摘要:一种是利用去找同一层的两个边,不断累加寄存。双指针法的思想先找到左右两边的第一个峰值作为参照位,然后分别向后向前每一步增加该位与参照位在这一位的差值,加入,直到下一个峰值,再更新为新的参照位。

Problem

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.

Example

Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

Note

有两种方法。一种是利用Stack去找同一层的两个边,不断累加寄存。如[2, 1, 0, 1, 2],2入栈,1入栈,0入栈,下一个1大于栈顶元素0,则计算此处的雨水量加入res,此过程中0从栈中弹出,1入栈,到下一个2,先弹出1,由于之前还有一个1在栈中,所以计算时高度的因数相减为0,雨水量为0,res无变化,继续pop出栈中的元素,也就是之前的1,此时stack中仍有元素2,说明左边还有边,继续计算底层高度为1,两个值为2的边之间的水量,加入res。
双指针法的思想:先找到左右两边的第一个峰值作为参照位,然后分别向后(向前)每一步增加该位与参照位在这一位的差值,加入sum,直到下一个峰值,再更新为新的参照位。这里有一个需要注意的地方,为什么要先计算左右两个峰值中较小的那个呢?因为在两个指针逼近中间组成最后一个积水区间时,要用较短边计算。

Solution

1. Stack

</>复制代码

  1. public class Solution {
  2. public int trapRainWater(int[] A) {
  3. Stack stack = new Stack();
  4. int res = 0;
  5. for (int i = 0; i < A.length; i++) {
  6. if (stack.isEmpty() || A[i] < A[stack.peek()]) stack.push(i);
  7. else {
  8. while (!stack.isEmpty() && A[i] > A[stack.peek()]) {
  9. int pre = stack.pop();
  10. if (!stack.isEmpty()) {
  11. res += (Math.min(A[i], A[stack.peek()]) - A[pre]) * (i - stack.peek() - 1);
  12. }
  13. }
  14. stack.push(i);
  15. }
  16. }
  17. return res;
  18. }
  19. }

2. Two-Pointer

</>复制代码

  1. public class Solution {
  2. public int trap(int[] A) {
  3. int left = 0, right = A.length-1, res = 0;
  4. while (left < right && A[left] < A[left+1]) left++;
  5. while (left < right && A[right] < A[right-1]) right--;
  6. while (left < right) {
  7. int leftmost = A[left], rightmost = A[right];
  8. if (leftmost < rightmost) {
  9. while (left < right && A[++left] < leftmost) res += leftmost - A[left];
  10. }
  11. else {
  12. while (left < right && A[--right] < rightmost) res += rightmost - A[right];
  13. }
  14. }
  15. return res;
  16. }
  17. }
双指针法update: 2018-3

</>复制代码

  1. public class Solution {
  2. public int trap(int[] A) {
  3. if (A == null || A.length < 3) return 0;
  4. //set left/right pointers
  5. int l = 0, r = A.length-1;
  6. //find the first left/right peaks as left/right bounds
  7. while (l < r && A[l] <= A[l+1]) l++;
  8. while (l < r && A[r] <= A[r-1]) r--;
  9. //output
  10. int trappedWater = 0;
  11. //implementation
  12. while (l < r) {
  13. int left = A[l];
  14. int right = A[r];
  15. //when you have a higher RIGHT bound...
  16. if (left <= right) {
  17. //when "l" is highest left bound
  18. //it"s safe to add trapped water RIGHTward
  19. while (l < r && left >= A[l+1]) {
  20. l++;
  21. trappedWater += left - A[l];
  22. }
  23. //when left pointer "l" found "l+1" a higher left bound
  24. //reset the left bound
  25. l++;
  26. }
  27. //when you have a higher LEFT bound...
  28. else {
  29. //when "r" is highest right bound
  30. //it"s safe to add trapped water LEFTward
  31. while (l < r && right >= A[r-1]) {
  32. r--;
  33. trappedWater += right - A[r];
  34. }
  35. //when right pointer "r" found "r-1" a higher right bound
  36. //reset the right bound
  37. r--;
  38. }
  39. }
  40. return trappedWater;
  41. }
  42. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65612.html

相关文章

  • [Leetcode] Trapping Rain Water 积水问题

    摘要:从右向左遍历时,记录下上次右边的峰值,如果左边一直没有比这个峰值高的,就加上这些差值。难点在于,当两个指针遍历到相邻的峰时,我们要选取较小的那个峰值来计算差值。所以,我们在遍历左指针或者右指针之前,要先判断左右两个峰值的大小。 Trapping Rain Water Given n non-negative integers representing an elevation map ...

    caohaoyu 评论0 收藏0
  • Leetcode[42] Trapping Rain Water

    摘要:复杂度思路因为蓄水多少取决于比较短的那块板的长度。代码复杂度思路考虑说明时候需要计算蓄水量当的时候,需要计算能储存的水的多少。每次还需要取出一个作为中间值。如果则一直向里面压进去值,不需要直接计算。 Leetcode[42] Trapping Rain Water Given n non-negative integers representing an elevation map ...

    jonh_felix 评论0 收藏0
  • leetcode42 Trapping Rain Water

    摘要:我先通过堆栈的方法,找到一个封闭区间,该区间可以盛水,该区间的右节点可以作为下一个封闭区间的起点。思路三堆栈的聪明使用在这里,堆栈允许我们渐进的通过横向分割而非之前传统的纵向分割的方式来累加计算盛水量。 题目要求 Given n non-negative integers representing an elevation map where the width of each bar...

    GitCafe 评论0 收藏0
  • 407. Trapping Rain Water II

    407. Trapping Rain Water II 题目链接:https://leetcode.com/problems... 参考discussion里的解法:https://discuss.leetcode.com/... 参考博客里的解释:http://www.cnblogs.com/grandy... public class Solution { public int tra...

    wenzi 评论0 收藏0
  • 42. Trapping Rain Water

    摘要:题目解答左边比右边小或者大都可以盛水,所以我们不能直接确定右边是否会有一个柱子比较大,能盛所有现在积攒的水。那么我们就找到中间最大的那个柱子,把它分成左右两边,那么不管从左边还是右边都能保证最后可以有最高的柱子在,之前盛的水都是有效的 题目:Given n non-negative integers representing an elevation map where the wid...

    seanlook 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<