您的位置:首页 > 编程语言 > C语言/C++

leetcode 日经贴,Cpp code -Maximal Rectangle

2015-04-18 16:40 513 查看
Maximal Rectangle

class Solution {
public:
int maxhis(const vector<int> hist) {
stack<pair<int,int> > st;
int maxarea = 0;
for (int i = 0; i <= hist.size(); ++i) {
int h = i == hist.size() ? -1:hist[i];
if (st.empty() || st.top().first < h) {
st.push(make_pair(h, i));
} else {
int lastp = i;
while (!st.empty() && st.top().first >= h) {
maxarea = max(maxarea, (i - st.top().second) * st.top().first);
lastp = st.top().second;
st.pop();
}
st.push(make_pair(h, lastp));
}
}
return maxarea;
}
int maximalRectangle(vector<vector<char> > &matrix) {
int n = matrix.size();
if (n == 0) return 0;
int m = matrix[0].size();
if (m == 0) return 0;
vector<int> hist(m, 0);
int marea = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
hist[j] = matrix[i][j]=='1'? hist[j] + 1: 0;
}
int ca = maxhis(hist);
marea = max(ca, marea);
}
return marea;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: