Maximal rectangle
Given a 2D binary matrix filled with 0‘s and 1‘s, find the largest rectangle containing all ones and return its area.
Solution Analysis:
The maximum rectangle area can be obtained in O (n) time.
If we regard each row as the X coordinate, the height is the number of 1 starting from that row.
Using the maximum rectangular area method, we can find the maximum rectangular area of each row in the O (n2) time.
Class solution {public: int maximalrectangle (vector <char> & matrix) {int nrow = matrix. size (); If (nrow = 0) return 0; int ncol = matrix. at (0 ). size (); STD: vector <int> TMP (ncol, 0); int result = 0; For (INT I = 0; I <nrow; ++ I) {// calculate the maximum rectangular area that can be formed for (Int J = 0; j <ncol; ++ J) {If (matrix. at (I ). at (j) = '0') {TMP. at (j) = 0;} else {TMP. at (j) ++; // calculate the cumulative height of the current column of the current row (number of 1)} int Area = Largestrectanglearea (TMP); Result = STD: max (result, area);} return result;} // maximum rectangular area int largestrectanglearea (STD :: vector <int> & height) {If (height. size () = 0) return 0; height. push_back (-1); STD: Stack <int> STK; int result = 0; For (INT I = 0; I
Leetcode: maximal rectangle-1 submatrix