Matrix Questions
- Maximal Square
- Count Square Submatrices with All Ones
- Bomb Enemy
Question
Original Question: Leetcode 221. Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
Example:
Input:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Output: 4
Solution
- Java
- Python
class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix.length == 0) return 0;
int[][] dp = new int[matrix.length+1][matrix[0].length+1];
int maxLen = 0;
for(int j = 1; j <= matrix.length; j++){
for(int i = 1; i <= matrix[0].length; i++){
if(matrix[j-1][i-1] == '1'){
dp[j][i] = Math.min(Math.min(dp[j][i-1], dp[j-1][i]), dp[j-1][i-1]) + 1;
maxLen = Math.max(maxLen, dp[j][i]);
}
}
}
return maxLen * maxLen;
}
}
class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
if not matrix or not matrix[0]:
return 0
m,n, maxL, prev = len(matrix), len(matrix[0]), 0, 0
sqLength = [0] *(n+1)
for i in range(1,m+1):
for j in range(1,n+1):
temp = sqLength[j]
if matrix[i-1][j-1] == '1':
sqLength[j] = min(sqLength[j], prev, sqLength[j-1]) + 1
maxL = max(maxL, sqLength[j])
else:
sqLength[j] = 0
prev = temp
return maxL*maxL
Question
Original Question: Leetcode 1277. Count Square Submatrices with All Ones
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Solution
- Java
- Python
class Solution {
public int countSquares(int[][] A) {
int res = 0;
for (int i = 0; i < A.length; ++i) {
for (int j = 0; j < A[0].length; ++j) {
if (A[i][j] > 0 && i > 0 && j > 0) {
A[i][j] = Math.min(A[i - 1][j - 1], Math.min(A[i - 1][j], A[i][j - 1])) + 1;
}
res += A[i][j];
}
}
return res;
}
}
;
Question
Original Question: Leetcode 361. Bomb Enemy
Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return the maximum enemies you can kill using one bomb. The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since the wall is too strong to be destroyed. Note: You can only put the bomb at an empty cell.
Example:
Input: [["0","E","0","0"],["E","0","W","E"],["0","E","0","0"]]
Output: 3
Explanation: For the given grid,
0 E 0 0
E 0 W E
0 E 0 0
Placing a bomb at (1,1) kills 3 enemies.
Solution
- Java
- Python
class Solution {
public int maxKilledEnemies(char[][] grid) {
if(grid.length == 0 || grid[0].length == 0) return 0;
int m = grid.length, n=grid[0].length;
int[][] up = new int[m][n];
int[][] down = new int[m][n];
int[][] left = new int[m][n];
int[][] right = new int[m][n];
// fill up;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] == 'W'){
up[i][j] = 0;
continue;
}
up[i][j] = grid[i][j] == 'E' ? 1 : 0;
if(i>0){
up[i][j] += up[i-1][j];
}
}
}
// fill left;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] == 'W'){
left[i][j] = 0;
continue;
}
left[i][j] = grid[i][j] == 'E' ? 1 : 0;
if(j>0){
left[i][j] += left[i][j-1];
}
}
}
// fill down;
for(int i = m-1; i >= 0; i--){
for(int j = n-1; j >= 0; j--){
if(grid[i][j] == 'W'){
down[i][j] = 0;
continue;
}
down[i][j] = grid[i][j] == 'E' ? 1 : 0;
if(i < m-1){
down[i][j] += down[i+1][j];
}
}
}
// fill right;
for(int i = m-1; i >= 0; i--){
for(int j = n-1; j >= 0; j--){
if(grid[i][j] == 'W'){
right[i][j] = 0;
continue;
}
right[i][j] = grid[i][j] == 'E' ? 1 : 0;
if(j < n-1){
right[i][j] += right[i][j+1];
}
}
}
int res = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] == '0'){
res = Math.max(res, up[i][j] + down[i][j] + right[i][j] + left[i][j]);
}
}
}
return res;
}
}