这两题看起来有点像,但是实际上是完全不一样的,区别在于:
The "Container With Most Water" solution will allow the water to rise above intermediate positions. With the "largest rectangle" problem, the rectangle cannot rise above intermediate bars.
也就是说Container With Most Water只考虑左右边界,[i,j]范围内的Area = min(height[i],height[j]) * (j-i);而Largest Rectangle in Histogram,高度最小值为[i,j]范围内所有高度的最小值。后者比前者要难很多
1.Container With Most Water
对于这题,考虑左右边界[i,j] ,当height[i]<height[j]时,因为i是其中的短板,则无论j取[i+1,j]中的任何值,Area都只会比当前已求出的[i,j]的Area小(横坐标范围减小,再遇见比height[i]更小的右边界),因此以i为左边界的情况不再考虑,i++;反之,j--,同样的思考方式。
代码:
public class Solution { public int min(int i,int j){ return i<j?i:j; } public int maxArea(int[] height) { // Note: The Solution object is instantiated only once and is reused by each test case. if(height == null) return 0; int i = 0; int j = height.length -1; int max = 0; while(i<j){ int area = min(height[i],height[j])*(j-i); if(area>max) max = area; if(height[i]<height[j]) i++; else j--; } return max; } }
Largest Rectangle in Histogram
代码如下:
public int largestRectangleArea(int[] height) { // Note: The Solution object is instantiated only once and is reused by each test case. if(height == null) return 0; int len = height.length; Stack<Integer> stack = new Stack<Integer>(); Stack<Integer> width = new Stack<Integer>();//记录向左可以扩展的宽度 int max = 0; stack.push(0); width.push(0); int h; for(int i = 0;i<=len;i++){ if(i == len) h = 0; else h = height[i]; int wid = 0; while(h<stack.peek()){ //1.算自己的左边界 //2.已放入栈中的高度>h的右边界已经找到了 int top = stack.pop(); wid += width.pop(); max = Math.max(max,top*wid); } stack.push(h); width.push(Math.max(wid+1,1));//每次加入stack的时候,他的左边界就已经确定了 } return max; }
相关推荐
在LeetCode这个著名的在线编程挑战平台上,有一道题目名为“Largest Rectangle in Histogram”(直译为“柱状图中的最大矩形”),这是一道与数据结构和算法密切相关的题目,旨在考察程序员对栈、动态规划以及几何...
python python_leetcode题解之084_Largest_Rectangle_in_Histogram
c c语言_leetcode题解之0084_largest_rectangle_in_histogram.zip
javascript js_leetcode题解之84-largest-rectangle-in-histogram.js
【LeetCode】Container With Most Water 是一道经典的计算机算法题,主要涉及到数组操作和动态规划的优化技巧。题目要求在给定的一组非负整数坐标中找到两条线,使得它们与x轴形成的容器能容纳最多的水。问题的核心...
在LeetCode上的问题"Container With Most Water"是一个经典的计算机科学问题,主要涉及到算法设计和优化。这个问题要求我们找到两个垂直线,它们与x轴形成一个容器,使得该容器能容纳最多的水。这个问题可以看作是二...
python python_leetcode题解之085_Maximal_Rectangle
c c语言_leetcode 0011_container_with_most_water.zip
javascript js_leetcode题解之85-maximal-rectangle.js
c语言基础 c语言_leetcode题解之0085_maximal_rectangle.zip
http://blog.csdn.net/two_water/article/details/53004027 LeetCode_直方图最大面积(Largest Rectangle in Histogram)
java入门 java_leetcode题解之011_Container_With_Most_Water
js js_leetcode题解之11-container-with-most-water.js
c语言入门 C语言_leetcode题解之11-container-with-most-water.c
java java_leetcode题解之Most Stones Removed with Same Row or Column.java
Container With Most Water LeetCode 19 Remove Nth Node From End of List LeetCode 42 Trapping Rain Water LeetCode 61 RotateList LeetCode 75 Sort Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum...
java java_leetcode题解之Most Common Word.java
【Golang】leetcode-golang,LeetCode solutions with Golang(Golang 实现) (Leetcode golang, LeetCode solutions with Golang (Golang implementation)) 【Golang】leetcode-golang,LeetCode solutions with Golang...
java java_leetcode题解之Most Profit Assigning Work.java
java java_leetcode题解之Most Frequent Subtree Sum.java