DP
https://leetcode.com/problems/maximal-rectangle/
class Solution {
public int maximalRectangle(char[][] matrix) {
int[] heights = new int[matrix[0].length];
int answer = 0;
for(int row=0; row<matrix.length; row++) {
for(int col=0; col<matrix[0].length; col++) {
if(matrix[row][col] == '1') {
heights[col]++;
} else {
heights[col] = 0;
}
int rightCol = col;
int curHeight = matrix.length;
for(int leftCol=col; leftCol>=0; leftCol--) {
int height = heights[leftCol];
int width = rightCol - leftCol + 1;
if(height == 0) {
break;
}
curHeight = Math.min(curHeight, height);
answer = Math.max(answer, curHeight * width);
}
}
}
return answer;
}
}
이미지 출처: https://leetcode.com/problems/maximal-rectangle/description/
0번째 컬럼 | 1번째 컬럼 | 2번째 컬럼 | 3번째 컬럼 | 4번째 컬럼 | |
---|---|---|---|---|---|
0번째 행 | 1 | 0 | 1 | 0 | 0 |
1번째 행 | 2 | 0 | 2 | 1 | 1 |
2번째 행 | 3 | 1 | 3 | 2 | 2 |
3번째 행 | 4 | 0 | 0 | 3 | 0 |
1시간 30분