leetCode解题报告5道题(十)(二)

2014-11-24 13:03:40 · 作者: · 浏览: 5
public class Solution {
    public int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        for (int row=m-1; row>=0; row--){
            for (int col=n-1; col>=0; col--){
                //右边的列的下标
                int colRight = col + 1;
                //下边的行的行标
                int rowDown = row + 1;
                //初始值
                int rowDownValue = Integer.MAX_VALUE;
                int colRightValue = Integer.MAX_VALUE;
                
                //存在行
                if (rowDown < m){
                    rowDownValue = grid[rowDown][col];
                }
                //存在列
                if (colRight < n){
                    colRightValue = grid[row][colRight];
                }
                //取最小
                int min = rowDownValue > colRightValue   colRightValue : rowDownValue;
                //最小等于Integer.MAX_VALUE则表示没有右边也没有下边元素,即为右下方的那个目标元素
                if (min != Integer.MAX_VALUE){
                    grid[row][col] += min;
                }
            }
        }
        return grid[0][0];
    }
}



题目五:Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

\

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

\

The largest rectangle is shown in the shaded area, which has area = 10 unit.< http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+CjwvcD4KPHA+CkZvciBleGFtcGxlLDxicj4KR2l2ZW4gaGVpZ2h0ID0gPGNvZGU+WzIsMSw1LDYsMiwzXTwvY29kZT4sPGJyPgpyZXR1cm4gPGNvZGU+MTA8L2NvZGU+LjwvcD4KPHA+PC9wPgo8cD48c3Ryb25nPrfWzvajujwvc3Ryb25nPjwvcD4KPHA+MaGisanBpreoo7o8L3A+CjxwPrP1v7TM4sS/o6y63Mjd0te4+MjLz+u1vbXEt723qL7NysexqcGmt6ijrNKyvs3Kx8u1ttTDv7j2IGluZGV4IDwgbGVuILa81dK1vbTTaW5kZXggzfnX8yC6zSDN+dPSo6zWsbW9aGVpZ2h0W2luZGV4XSA+IGhlaWdodFtsZWZ0XSAgus0gaGVpZ2h0W2luZGV4XSA+IGhlaWdodFtyaWdodF0sIMi7uvMgsNFpbnQgdGVtcCA9IGhlaWdodFtpbmRleF0gKiAocmlnaHQgLSBsZWZ0ICYjNDM7IDEpIKOsIMjnuft0ZW1wID4gbWF4LCDU8rj80MJtYXi1xCYjMjA1NDA7o6y1q8rH1eLR+bXEu7DSu8/C19O+zVRMRbOsyrHBy6GjPC9wPgo8cD48c3Ryb25nPlRMRbT6wuujujwvc3Ryb25nPjxicj4KPC9wPgo8cD48L3A+CjxwcmUgY2xhc3M9"brush:java;">public class Solution { public int largestRectangleArea(int[] height) { int len = height.length; int max = Integer.MIN_VALUE; for (int i=0; i =0){ if (height[left] < nowHeight){ break; } --left; } int right=i+1; while (right max){ max = allSize; } } return max; } }


2、借助辅助栈:

居然暴力法不行,那我们可以知道它主要的时间消耗其实是在寻找左边的left, 和右边的right上面,这样的话有没有什么办法把这块时间节省下来,在我们遍历的过程中确定好left, right的值呢?

我们采用辅助栈( 栈中存放矩形的下标 )来做这道题目可以解决它哈。

算法描述:

1、定义一个空栈

2、遍历数组 index < n

2.1、遍历到index的时候,如果“栈为空”或者height[stack.peek()] <= height[index], 这时候把 index 压入到stack中,并index++

2.2、如果不满足2.1的情况,那么证明栈顶元素p对应的height值比当前index对应的height值来得小,这样的话弹出栈顶元素,并算出以栈顶元素为height所能得到的最大的矩形面积,这时候就是用辅助栈的关键地方了,针对这个情况,它的left位置就是它在栈中的下一个位置(如果没有,则表示left是0,即左边界从0开始),而它的右边界right位置就是当前index的值。

看到这里,肯定迷糊了吧,想想只有当 “栈为空”或者height[stack.peek()] <= height[index] 的时候我们才会把index值压入栈中,也就是说如果把栈从上往下看,必定上面的值对应的height会比下面的大或者相等,这样下面的那个元素对应的值就必定是left的值,同理因为当 当前height[stack.peek()] > height[index] , 我们才进行计算面积的工作,这样子的话这个index就是stack.peek()取到的这个位置对应height 的right右边界咯.


AC代码:

public class Solution {
    public int largestRectangleArea(int[] height) {
        int len = height.length;
        if (len == 0)
            return 0;
        int max = Integer.MIN_VALUE;
        Stack
  
    stack = new Stack
   
    (); int index = 0; while (index < len){ if (stack.empty() || height[(int)stack.peek()] <= height[index]){ stack.push(index); index++;//这里才有++哦! }else{ //计算面积 int popIndex = (int)stack.pop(); int allWidth = stack.empty()   index : index-((int)stack.peek())-1; int tempValue = height[popIndex] * allWidth; if (tempValue > max){ max = tempValue; } } } //right边界都为len while (!stack.empty()){ int popIndex = (int)stack.pop(); int allWidth = stack.empty()   len : len-((int)stack.peek())-1; int tempValue = height[popIndex] * allWidth; if (tempValue > max){ max = tempValue; } } return max; } }