* leetcode #85 in cpp
2016-06-12 03:43
483 查看
Solution:
This question is hard. I did not know how to solve this until I looked up the solution online. Basically to solve this question we employ techniques in #84. That is, we treat this problem as looking for largest rectangle in histogram.
Example:
given
We treat each row as the x-axis, and the 1s as the height of the histogram.
For row 1: it is [0, 1,1], max rectangle in this histogram is 2.
For row 2: it is [1,2,2], max rect in this histogram is 4;
For row 3: it is [2,0,3], max rect in this histogram is 3;
Thus we first need to transform the matrix into histograms based on each row(step1), then we use #84 to find out the max rect in the histograms.
Code:
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if(matrix.empty()) return 0;
int m = matrix.size();
int n = matrix[0].size();
vector<vector<int>> hist(m, vector<int>(n,0));
<span style="white-space:pre"> </span>// step 1
for(int i = 0; i < m; i ++){
for(int j = 0; j < n; j ++){
if(matrix[i][j] == '1'){
if(i-1>=0) hist[i][j] = hist[i-1][j] + 1;
else hist[i][j] = 1;
}
}
}
<span style="white-space:pre"> </span>
int max_area = 0;
for(int i = 0; i < m; i ++){
max_area = max(max_area,largestRectangleArea(hist[i]));
}
return max_area;
}
int largestRectangleArea(vector<int>& heights) {
heights.push_back(0);
stack<int> left_ind;
int max_area = 0;
int i = 0;
while(i < heights.size()){
if(left_ind.empty()) {
left_ind.push(i);
i++;
}
else{
if(heights[i] > heights[left_ind.top()]){
left_ind.push(i);
i++;
}else{
int left = left_ind.top();
left_ind.pop();
max_area = max(max_area, heights[left] *(left_ind.empty()? i :(i - left_ind.top() - 1)));
}
}
}
return max_area;
}
};
This question is hard. I did not know how to solve this until I looked up the solution online. Basically to solve this question we employ techniques in #84. That is, we treat this problem as looking for largest rectangle in histogram.
Example:
given
0 | 1 | 1 |
1 | 1 | 1 |
1 | 0 | 1 |
For row 1: it is [0, 1,1], max rectangle in this histogram is 2.
For row 2: it is [1,2,2], max rect in this histogram is 4;
For row 3: it is [2,0,3], max rect in this histogram is 3;
Thus we first need to transform the matrix into histograms based on each row(step1), then we use #84 to find out the max rect in the histograms.
Code:
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if(matrix.empty()) return 0;
int m = matrix.size();
int n = matrix[0].size();
vector<vector<int>> hist(m, vector<int>(n,0));
<span style="white-space:pre"> </span>// step 1
for(int i = 0; i < m; i ++){
for(int j = 0; j < n; j ++){
if(matrix[i][j] == '1'){
if(i-1>=0) hist[i][j] = hist[i-1][j] + 1;
else hist[i][j] = 1;
}
}
}
<span style="white-space:pre"> </span>
int max_area = 0;
for(int i = 0; i < m; i ++){
max_area = max(max_area,largestRectangleArea(hist[i]));
}
return max_area;
}
int largestRectangleArea(vector<int>& heights) {
heights.push_back(0);
stack<int> left_ind;
int max_area = 0;
int i = 0;
while(i < heights.size()){
if(left_ind.empty()) {
left_ind.push(i);
i++;
}
else{
if(heights[i] > heights[left_ind.top()]){
left_ind.push(i);
i++;
}else{
int left = left_ind.top();
left_ind.pop();
max_area = max(max_area, heights[left] *(left_ind.empty()? i :(i - left_ind.top() - 1)));
}
}
}
return max_area;
}
};
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- C++中const用法总结
- CPP 虚函数、虚函数表及虚拟继承(转)
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- Deploying Control Plane Policing
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解