leetcode_maximal_rectangle 发表于 2016-12-11 难度:Medium 此题是柱形图最大长方形面积的扩展,在每一行,向上进行扩展,’1’为阴影。 123456789101112131415161718192021222324252627282930313233class Solution {public: int maximalRectangle(vector<vector<char>>& matrix) { if(matrix.size()==0 || matrix[0].size()==0) return 0; vector<int> heights(matrix[0].size()+1,0); int res=0; for(int i = 0; i < matrix.size(); i++) { matrix[i].push_back('0'); stack<int> s; for(int j = 0; j < matrix[i].size(); j++) { if(matrix[i][j] == '1') { heights[j]++; } else heights[j] = 0; while(!s.empty() && heights[s.top()] >= heights[j]) { int cur_j = s.top(); s.pop(); int area = heights[cur_j]*(s.empty()?j:(j-s.top()-1)); res = max(res,area); } s.push(j); } } return res; }};