zoukankan      html  css  js  c++  java
  • leetcode64:maximal-rectangle

    题目描述

    给出一个只包含0和1的二维矩阵,找出最大的全部元素都是1的长方形区域,返回该区域的面积。

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
    class Solution {
    public:
        int maximalRectangle(vector<vector<char> > &matrix) {
            if (matrix.size()==0)
                return 0;
            int m=matrix.size();
            int n=matrix[0].size();
            vector< int> h(n);
            int maxS=0;
            int num;
            stack<int> st;
            st.push(-1);
            for (int i=0;i<m;i++)
            {
                for (int j=0;j<n;j++){
                    if (matrix[i][j]=='1')
                        h[j]++;
                    else
                        h[j]=0;
                    
                }
                
                for (int j=0;j<n;j++){
                    while (st.top()!=-1 && h[j] <h[st.top()])
                    {
                        num=st.top();
                        st.pop();
                        maxS=max(maxS,(j-1-st.top())*h[num]);
                        
                    }
                    st.push(j);
                }
                while (st.top()!=-1){
                    num=st.top();
                    st.pop();
                    maxS=max(maxS,(n-1-st.top())*h[num]);
                }
            }
            return maxS;
        }
    };
  • 相关阅读:
    js中两个==和三个===的区别
    软件需求工程解析
    《我们应当怎样做需求分析》阅读笔记
    需求工程阅读笔记03
    个人小软件冲刺05
    个人小软件冲刺04
    需求工程阅读笔记02
    个人小软件冲刺03
    个人小软件冲刺02
    个人小软件冲刺01
  • 原文地址:https://www.cnblogs.com/hrnn/p/13428018.html
Copyright © 2011-2022 走看看