[LeetCode] 85. Maximal Rectangle Java

來源:互聯網
上載者:User

標籤:儲存   mat   16px   amp   solution   ++   font   span   長條圖   

題目:

Given a 2D binary matrix filled with 0‘s and 1‘s, find the largest rectangle containing only 1‘s and return its area.

For example, given the following matrix:

1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0

Return 6.

題意及分析:就是給一個矩陣,找一個全是一的最大子矩陣。給出一個包含0,1的二維矩陣,要求求出只包含1的最大子矩陣。判斷能不能在matrix[i][j]及其坐左上方的點形成一個全是1的矩形:若當前點為0,肯定不可以;若當前點不為0,那麼如何求左上方能得到的全是1的最大矩陣,這裡可以轉化為求長條圖的最大面積。

代碼:

class Solution {    public int maximalRectangle(char[][] matrix) {      //給出一個包含0,1的二維矩陣,要求求出只包含1的最大子矩陣。判斷能不能在matrix[i][j]及其坐左上方的點形成一個全是1的矩形:若當前點為0,肯定不可以;若當前點不為0,        int xLength = matrix.length;        if(xLength==0) return 0;        int yLength = matrix[0].length;        int[][]  tempMatrix = new int[xLength][yLength];        //tempMatrix[i][j]代表在在第j列到底i行時連續1的個數        for(int j=0;j<yLength;j++){            tempMatrix[0][j] = (matrix[0][j]==‘0‘?0:1);        }        for(int j=0;j<yLength;j++) {            for(int i=1;i<xLength;i++){                if(matrix[i][j]==‘0‘)                    tempMatrix[i][j] = 0;                else{                    tempMatrix[i][j] = tempMatrix[i-1][j]+1;                }            }        }        int max = 0;        for(int i=0;i<xLength;i++){            int temp = largestRectangleArea(tempMatrix[i]);            if(max <temp)                max = temp;        }        return max;    }    private int largestRectangleArea(int[] heights) {        //如果已知height數組是升序的,應該怎麼做?比如1,2,5,7,8那麼就是(1*5) vs. (2*4) vs. (5*3) vs. (7*2) vs. (8*1)也就是max(height[i]*(size-i)),使用棧的目的就是構造這樣的升序序列,按照以上方法求解。在構造棧的時候儲存可能的最大值        if(heights == null)            return 0;        int tempResult = 0;        Stack<Integer> stack = new Stack<>();        stack.push(heights[0]);        for(int i=1;i<heights.length;i++){            if(heights[i]>=stack.peek()){        //升序                stack.push(heights[i]);            }else{                if(!stack.isEmpty()){                    int count = 0;                    int min = stack.peek();                    while(!stack.isEmpty() && stack.peek()>heights[i]){                        if(stack.peek()<min){                            min = stack.peek();                        }                        stack.pop();                        count ++;                        if(tempResult<count*min){                            tempResult = count*min;                        }                    }                    int j=0;                    while(j<=count){                        stack.push(heights[i]);                        j++;                    }                }            }        }        for(int i=heights.length-1;i>=0;i--){            int x= stack.pop();            if((heights.length-i)*x>tempResult){                tempResult =(heights.length-i)*x;            }        }        return tempResult;    }}

 

[LeetCode] 85. Maximal Rectangle Java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.