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.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1
题目链接:https://leetcode.com/problems/count-square-submatrices-with-all-ones/
题目大意:给一个n*m的01矩阵,求1组成的正方形的个数
题目分析:比较裸的dp,最无脑的方法:dp[i][j][k]表示是否能以(i,j)为右下顶点组成边长为k的正方形,同时记录每个点向上,向左能延伸的最长长度,则dp[i][j][k]可行的条件为当前点值为1且up[i][j] >= k,left[i][j] >= k且dp[i - 1][j - 1][k - 1]为true
30ms,时间击败9.78%
class Solution {
public int countSquares(int[][] matrix) {
int n = matrix.length;
int m = matrix[0].length;
int l = Math.min(n, m);
boolean[][][] dp = new boolean[n][m][l + 1];
int[][] left1 = new int[n][m];
int[][] up1 = new int[n][m];
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == 0) {
left1[i][j] = 0;
up1[i][j] = 0;
} else {
ans++;
dp[i][j][1] = true;
left1[i][j] = 1 + (j > 0 ? left1[i][j - 1] : 0);
up1[i][j] = 1 + (i > 0 ? up1[i - 1][j] : 0);
}
}
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
for (int k = 2; k <= l; k++) {
if (matrix[i][j] == 1 && up1[i][j] >= k && left1[i][j] >= k && dp[i - 1][j - 1][k - 1]) {
dp[i][j][k] = true;
ans++;
} else {
break;
}
}
}
}
return ans;
}
}
容易发现可以将k直接作为dp值,及dp[i][j]表示以点(i,j)为右下顶点所能组成的正方形的最长边长,对于每个值为1的点,根据up,left及dp[i - 1][j - 1]的值不难推出能组成的正方形的最长边长,需要注意的dp[i][j]除了上述定义外还有个含义是以点(i,j)为右下顶点所能组成的正方形的个数,因此最终只要累加所有的dp值即可
10ms,时间击败18.77%
class Solution {
public int countSquares(int[][] matrix) {
int n = matrix.length;
int m = matrix[0].length;
int[][] dp = new int[n][m];
int[][] left1 = new int[n][m];
int[][] up1 = new int[n][m];
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == 0) {
left1[i][j] = 0;
up1[i][j] = 0;
} else {
dp[i][j] = 1;
left1[i][j] = 1 + (j > 0 ? left1[i][j - 1] : 0);
up1[i][j] = 1 + (i > 0 ? up1[i - 1][j] : 0);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int maxL = Math.min(left1[i][j], up1[i][j]);
if (i > 0 && j > 0 && dp[i - 1][j - 1] <= maxL - 1) {
maxL = dp[i - 1][j - 1] + 1;
}
dp[i][j] = maxL;
ans += dp[i][j];
}
}
return ans;
}
}
再进一步可以发现,如果不需要保留matrix数组,可以直接用matrix来替代up个left,将matrix[i][j]定义为,点(i,j)可以向上或向左延伸的最短长度
7ms,时间击败32.50%
class Solution {
public int countSquares(int[][] matrix) {
int n = matrix.length;
int m = matrix[0].length;
int[][] dp = new int[n][m];
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == 1) {
dp[i][j] = 1;
int l = 1 + (j > 0 ? matrix[i][j - 1] : 0);
int r = 1 + (i > 0 ? matrix[i - 1][j] : 0);
matrix[i][j] = Math.min(l, r);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int maxL = matrix[i][j];
if (i > 0 && j > 0 && dp[i - 1][j - 1] <= maxL - 1) {
maxL = dp[i - 1][j - 1] + 1;
}
dp[i][j] = maxL;
ans += dp[i][j];
}
}
return ans;
}
}
其实还能再进一步优化,直接把dp数组也省掉,用matrix数组来代替dp数组,这里就不记了