85. Maximal Rectangle
Posted The Tech Road
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了85. Maximal Rectangle相关的知识,希望对你有一定的参考价值。
https://leetcode.com/problems/maximal-rectangle/description/
class Solution { public: int maximalRectangle(vector<vector<char>>& matrix) { int m = matrix.size(); if (m == 0) return 0; int n = matrix[0].size(); if (n == 0) return 0; vector<int> v(n, 0); int res = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (matrix[i][j] == ‘0‘) v[j] = 0; else v[j]++; } res = max(res, helper(v)); } return res; } int helper(const vector<int>& v) { stack<int> s; int res = 0; for (int i = 0; i <= v.size(); i++) { int cur = i == v.size() ? 0 : v[i]; while (!s.empty() && cur < v[s.top()]) { int idx = s.top(); s.pop(); int w = s.empty() ? i : i - s.top() - 1; res = max(res, w * v[idx]); } s.push(i); } return res; } };
以上是关于85. Maximal Rectangle的主要内容,如果未能解决你的问题,请参考以下文章