首页 > 学院 > 开发设计 > 正文

Maximal Rectangle

2019-11-08 19:45:05
字体:
来源:转载
供稿:网友

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

int fun(vector<vector<char> > &matrix){	int m = matrix.size();	int n = matrix[0].size();	vector<vector<int> > width(m, vector<int>(n));	if (matrix[0][0] == '1')	{		width[0][0] = 1;	}	else	{		width[0][0] = 0;	}	for (int i = 1; i < n; i++)	{		if (matrix[0][i] == '0')		{			width[0][i] = 0;		}		else		{			width[0][i] = 1 + width[0][i-1];		}	}	for (int i = 1; i < m; i++)	{		if (matrix[i][0] == '0')		{			width[i][0] = 0;		}		else		{			width[i][0] = 1;		}	}	for (int i = 1; i < m; i++)	{		for (int j = 1; j < n; j++)		{			if (matrix[i][j] == '0')			{				width[i][j] = 0;			}			else			{				width[i][j] = 1 + width[i][j-1];			}		}	}	int result = 0;	for (int i = 0; i < m; i++)	{		for (int j = 0; j < n; j++)		{			int cur = width[i][j];			int curWidth = width[i][j];			for (int k = i-1; k >= 0; k--)			{				int curHeight = i - k + 1;				curWidth = min(curWidth, width[k][j]);				int temp = curHeight * curWidth;				if (temp > cur)				{					cur = temp;				}			}			if (cur > result)			{				result = cur;			}		}	}	return result;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表