85. Maximal Rectangle

85. Maximal Rectangle

题目

 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 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

Return 6. 

解析

// 85. Maximal Rectangle
class Solution_85 {
public:

	int largestRectangleArea(vector<int> &height) {
		int res = 0;
		stack<int> s;
		height.push_back(0);
		for (int i = 0; i < height.size(); ++i) {
			if (s.empty() || height[s.top()] <= height[i]) s.push(i);
			else {
				int tmp = s.top();
				s.pop();
				res = max(res, height[tmp] * (s.empty() ? i : (i - s.top() - 1)));
				--i;
			}
		}
		return res;
	}

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

		if (matrix.empty())
		{
			return 0;
		}
		int res = 0;

		int n = matrix.size();
		int m = matrix[0].size();

		vector<int> height(m);
		for (int i = 0; i < n;i++)
		{
			for (int j = 0; j < m;j++)
			{
				height[j] = matrix[i][j] == '0' ? 0 : (1+height[j]);
			}
			res =max(res, largestRectangleArea(height));
		}

		return res;
	}
};

题目来源

原文地址:https://www.cnblogs.com/ranjiewen/p/8782409.html