[LeetCode]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!
题意:求由上面的数组组成的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; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。