intSolution::largestRectangleArea(vector<int>& heights) { int res = 0; stack<pair<int, int>> s; for (int i = 0; i < heights.size(); i++) { int index = i; while (!s.empty() && s.top().second >= heights[i]) { index = s.top().first; res = max(res, (i - index) * s.top().second); s.pop(); } s.push({index, heights[i]}); } while (!s.empty()) { res = max(res, (int)(s.top().second * (heights.size() - s.top().first))); s.pop(); } return res; }
intSolution::maximalRectangle(vector<vector<char>>& matrix) { int res = 0; if (!matrix.empty()) { int m = matrix.size(); int n = matrix[0].size(); vector<int> h(n, 0); 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; } } res = max(res, largestAreaAboveLine(h)); } } return res; };
intSolution::largestAreaAboveLine(vector<int>& heights) { int res = 0; stack<pair<int, int>> s; for (int i = 0; i < heights.size(); i++) { int index = i; while (!s.empty() && s.top().second >= heights[i]) { index = s.top().first; res = max(res, (i - index) * s.top().second); s.pop(); } s.push({index, heights[i]}); } while (!s.empty()) { res = max(res, (int)(s.top().second * (heights.size() - s.top().first))); s.pop(); } return res; }