|
| 1 | +//Problem : Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. |
| 2 | + |
| 3 | + |
| 4 | +#include <string> |
| 5 | +#include <iostream> |
| 6 | +#include <vector> |
| 7 | +#include <cmath> |
| 8 | +#include <climits> |
| 9 | +#include <stack> |
| 10 | + |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +// credit to http://www.geeksforgeeks.org/largest-rectangle-under-histogram/ |
| 14 | +int getMaxArea(vector<int> hist, int n) |
| 15 | +{ |
| 16 | + // Create an empty stack. The stack holds indexes of hist[] array |
| 17 | + // The bars stored in stack are always in increasing order of their |
| 18 | + // heights. |
| 19 | + stack<int> s; |
| 20 | + |
| 21 | + int max_area = 0; // Initalize max area |
| 22 | + int tp; // To store top of stack |
| 23 | + int area_with_top; // To store area with top bar as the smallest bar |
| 24 | + |
| 25 | + // Run through all bars of given histogram |
| 26 | + int i = 0; |
| 27 | + while (i < n) |
| 28 | + { |
| 29 | + // If this bar is higher than the bar on top stack, push it to stack |
| 30 | + if (s.empty() || hist[s.top()] <= hist[i]) |
| 31 | + s.push(i++); |
| 32 | + |
| 33 | + // If this bar is lower than top of stack, then calculate area of rectangle |
| 34 | + // with stack top as the smallest (or minimum height) bar. 'i' is |
| 35 | + // 'right index' for the top and element before top in stack is 'left index' |
| 36 | + else |
| 37 | + { |
| 38 | + tp = s.top(); // store the top index |
| 39 | + s.pop(); // pop the top |
| 40 | + |
| 41 | + // Calculate the area with hist[tp] stack as smallest bar |
| 42 | + area_with_top = hist[tp] * (s.empty() ? i : i - s.top() - 1); |
| 43 | + |
| 44 | + // update max area, if needed |
| 45 | + if (max_area < area_with_top) |
| 46 | + max_area = area_with_top; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Now pop the remaining bars from stack and calculate area with every |
| 51 | + // popped bar as the smallest bar |
| 52 | + while (s.empty() == false) |
| 53 | + { |
| 54 | + tp = s.top(); |
| 55 | + s.pop(); |
| 56 | + area_with_top = hist[tp] * (s.empty() ? i : i - s.top() - 1); |
| 57 | + |
| 58 | + if (max_area < area_with_top) |
| 59 | + max_area = area_with_top; |
| 60 | + } |
| 61 | + |
| 62 | + return max_area; |
| 63 | +} |
| 64 | + |
| 65 | +int main(){ |
| 66 | + int row,col; |
| 67 | + cin>>row>>col; |
| 68 | + int check; |
| 69 | + vector< vector<int> > matrix(row, vector<int>(col, 0)); |
| 70 | + vector<int> table(col); |
| 71 | + for(int r=0;r<row;r++){ |
| 72 | + for(int c=0;c<col;c++){ |
| 73 | + cin>>matrix[r][c]; |
| 74 | + if(r==0) |
| 75 | + table[c]=matrix[r][c]; |
| 76 | + } |
| 77 | + } |
| 78 | + int max=0; |
| 79 | + for(int r=0;r<row;r++){ |
| 80 | + for(int c=0;c<col;c++) |
| 81 | + { |
| 82 | + if(matrix[r][c]==0) |
| 83 | + table[c]=0; |
| 84 | + else |
| 85 | + table[c]+=1; |
| 86 | + } |
| 87 | + check=getMaxArea(table,table.size()); |
| 88 | + if(max<check) |
| 89 | + max=check; |
| 90 | + } |
| 91 | + cout<<max<<endl; |
| 92 | +} |
0 commit comments