标签:input == amp int ima solution push bsp ret
https://www.cnblogs.com/grandyang/p/4322667.html
把矩形分成一行一行送入子函数获得每行的最大值,然后再比较各行的最大值获得总的最大值
送入的每行不是0、1组成,而是每行的高度,这样就转换成了largest rectangle in histogram的问题
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; int res = 0; vector<int> input(n); for(int i = 0;i < m;i++){ for(int j = 0;j < n;j++) input[j] = (matrix[i][j] == ‘0‘ ? 0 : 1 + input[j]); res = max(res,maximal(input)); } return res; } int maximal(vector<int> matrix){ stack<int> s; int res = 0; matrix.push_back(0); for(int i = 0;i < matrix.size();i++){ if(s.empty() || matrix[s.top()] < matrix[i]) s.push(i); else{ int num = s.top(); s.pop(); res = max(res,matrix[num] * (s.empty() ? i : (i - s.top() - 1))); i--; } } return res; } };
标签:input == amp int ima solution push bsp ret
原文地址:https://www.cnblogs.com/ymjyqsx/p/10520105.html