Data structure -- monotonic stack -- find the size of the largest submatrix

Find the size of the largest sub-matrix
Given an integer matrix map, in which the values ​​are only 0 and 1, find the number of 1s in the largest rectangular area among all the rectangular areas in which all are 1.
For example:
1 1 1 0
Among them, the largest rectangular area has 3 1s, so return 3.
Another example:
1 0 1 1
1 1 1 1
1 1 1 0
Among them, the largest rectangular area has 6 1s, so 6 is returned.

Solution: Put it into a matrix, and start from the 0th row to calculate the maximum area of ​​the histogram when this row is used as the bottom

For example, in line 0, the array is [1, 0, 1, 1]. At this time, find the maximum area of ​​the histogram according to the following.

Then use the first row as the base, at this time the array is [2, 1, 2, 2], and similarly find the maximum area of ​​the histogram

Then start with the second row, and the array is [3, 2, 3, 0

Note: The size of the array is calculated according to how many consecutive 1s there are in a column starting from its row.

 



 

Similar topic: Given an array representing the height of the histogram at each location, find the maximum area of ​​the contiguous part of the histogram

Solution: Use a monotone stack to form a structure from the bottom of the stack to the top of the stack. When the element num to be pushed into the stack is smaller than the element at the top of the stack, the element at the top of the stack is popped out of the stack. Record, so that the area centered on this element can be found.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325250674&siteId=291194637