problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::maximal_rectangle::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static int maximalRectangle (vector< vector< char > > &matrix)
 

详细描述

在文件 leetcode.h3102 行定义.

成员函数说明

◆ maximalRectangle()

int leetcode::maximal_rectangle::Solution::maximalRectangle ( vector< vector< char > > &  matrix)
static

在文件 leetcode.cpp8776 行定义.

8776 {
8777 const int m = matrix.size();
8778 if(m == 0) {
8779 return 0;
8780 }
8781 const int n = matrix[0].size();
8782 vector left(m, vector(n, 0));
8783
8784 for(int i = 0; i < m; i++) {
8785 for(int j = 0; j < n; j++) {
8786 if(matrix[i][j] == '1') {
8787 left[i][j] = (j == 0 ? 0 : left[i][j - 1]) + 1;
8788 }
8789 }
8790 }
8791
8792 int ret = 0;
8793 for(int j = 0; j < n; j++) {
8794 // 对于每一列,使用基于柱状图的方法
8795 vector<int> up(m, 0), down(m, 0);
8796
8797 stack<int> stk;
8798 for(int i = 0; i < m; i++) {
8799 while(!stk.empty() && left[stk.top()][j] >= left[i][j]) {
8800 stk.pop();
8801 }
8802 up[i] = stk.empty() ? -1 : stk.top();
8803 stk.push(i);
8804 }
8805 stk = stack<int>();
8806 for(int i = m - 1; i >= 0; i--) {
8807 while(!stk.empty() && left[stk.top()][j] >= left[i][j]) {
8808 stk.pop();
8809 }
8810 down[i] = stk.empty() ? m : stk.top();
8811 stk.push(i);
8812 }
8813
8814 for(int i = 0; i < m; i++) {
8815 const int height = down[i] - up[i] - 1;
8816 int area = height * left[i][j];
8817 ret = max(ret, area);
8818 }
8819 }
8820 return ret;
8821 }

引用了 acwing::acwing1929::down, acwing::acwing1929::left , 以及 acwing::acwing1929::up.

被这些函数引用 leetcode::maximal_rectangle::TEST().


该类的文档由以下文件生成: