This question is more difficult than it was just now. If you have never done the last one, it is heartless.
First think of a stupid solution, how to determine a rectangle? Find a top left corner and check the position where each row can be extended. Note that as the number of rows increases, the number of columns can only be shortened and cannot be extended. I want to know the complexity of this method, and the timeout is no doubt.
Will it be inspired if I just did this question of seeking for a column. Every 1 in the matrix is regarded as a small square. In each column, continuous 1 forms a column, the maximum area enclosed by a series of such columns is the largest rectangle of all 1, and the problem is perfectly transformed. Although in my opinion, this kind of conversion is very difficult. If these two questions are not adjacent, it is too difficult to think about them. This gives us a good lesson. There may be some relationship between different shapes.
How to construct such a column is much simpler, and each row needs to be calculated. If the row is 1 or 0 at the top of the current row and 1 at the top of the row, the column length increases by 1 on the top of the corresponding column, if it is 0, it is 0. Returns the largest area.
class Solution {public: int largestArea(int *height, int length){ stack
s; int i=0, res=0, tpres, tph; while(i
> &matrix) { int m = matrix.size(); if(m == 0) return 0; int n = matrix[0].size(); int **h = new int*[m]; for(int i=0;i