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; } };
相关文章推荐
- leetcode 日经贴,Cpp code -House Robber II
- leetcode 日经贴,Cpp code -Maximal Square
- leetcode 日经贴,Cpp code -Bitwise AND of Numbers Range
- leetcode 日经贴,Cpp code -Majority Element
- leetcode 日经贴,Cpp code -Path Sum
- leetcode 日经贴,Cpp code -Recover Binary Search Tree
- leetcode 日经贴,Cpp code -Path Sum II
- leetcode 日经贴,Cpp code -Permutations II
- leetcode 日经贴,Cpp code -Shortest Palindrome
- leetcode 日经贴,Cpp code -The Skyline Problem
- leetcode 日经贴,Cpp code -Contains Duplicate III
- leetcode 日经贴,Cpp code -Remove Duplicates from Sorted List II
- leetcode 日经贴,Cpp code -Reorder List
- leetcode 日经贴,Cpp code -Minimum Depth of Binary Tree
- leetcode 日经贴,Cpp code -Merge k Sorted Lists
- leetcode 日经贴,Cpp code -Pow(x, n)
- leetcode 日经贴,Cpp code -Permutations
- leetcode 日经贴,Cpp code -Restore IP Addresses
- leetcode 日经贴,Cpp code -Combination Sum III
- leetcode 日经贴,Cpp code -Sliding Window Maximum