【java-np】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.

Constraints:

1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1

hint1:Create an additive table that counts the sum of elements of submatrix with the superior corner at (0,0).
hint2:Loop over all subsquares in O(n^3) and check if the sum make the whole array to be ones, if it checks then add 1 to the answer.

answer one

    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;
    }

some clarity on the explanation:
‘A’ given matrix and ‘dp’ dynamic matrix we are building,

If A[i][j]=1 we claim dp[i][j] = min(dp[i-1],[j], dp[i][j-1], dp[i-1][j-1]) + 1
above holds true because,
Imagine you have a square of size k where (i,j) is in the right down corner of it.
Then dp[i-1][j] is at least of size k-1 (as a sub square) and the same is true for dp[i][j-1] and dp[i-1],[j-1]
therefore min(dp[i-1],[j], dp[i][j-1], dp[i-1][j-1]) is at least k-1.

// Given matrix
[
[1,0,1],
[1,1,0],
[1,1,0]
]

// DP matrix
we fill the first row and first column with zeros and a cell is zero if A[i][j] = 0 (as this can’t make a square matrix at all)
for A[i][j]>0 case we use the dp relation A[i][j] += min(A[i - 1][j - 1], min(A[i - 1][j], A[i][j - 1]));

0 0 0 0
0 1 0 1
0 1 1 0
0 1 2 0

hope this helps.

answer two

class Solution {
    public int countSquares(int[][] matrix) {
        
        int n = matrix.length;
        int m = matrix[0].length;
        int[][] dp = new int[n][m];
        int count = 0;
        
        for(int i = 0; i < n; i++) if(matrix[i][0] == 1) dp[i][0] = 1;
        for(int j = 0; j < m; j++) if(matrix[0][j] == 1) dp[0][j] = 1;
		
        // dp[i][j] will tell you the max side of the square with bottom right corner at (i, j).
        for(int i = 1; i < n; i++){
            for(int j = 1; j < m; j++){
                if(matrix[i][j] == 1){
                    int min = 1 + Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1]));
                    dp[i][j] = min;
                }
            }
        }
        
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++) count += dp[i][j];
        }
        
        return count;
        
    }
}

参考

answer three

参考

answer four

更清晰的解释
参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java代码示例: ```java public class MatrixMultiplication { public static void main(String[] args) { int n = 8; // n阶矩阵 int m = (int) Math.sqrt(n); // m为n的平方根 int k = 1; while (n % 2 == 0) { // 寻找n的因子2的个数k n /= 2; k *= 2; } int[][] matrix1 = new int[n][n]; // 第一个矩阵 int[][] matrix2 = new int[n][n]; // 第二个矩阵 // 初始化矩阵元素 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { matrix1[i][j] = i * j; // 可以根据实际需求修改 matrix2[i][j] = i + j; // 可以根据实际需求修改 } } int[][][] submatrices = new int[m][m][k]; // 存储m*m个子矩阵 // 按照要求划分子矩阵 for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { for (int p = 0; p < k; p++) { submatrices[i][j][p] = matrix1[i * k + p][j * k] * matrix2[j * k][i * k + p]; } } } // 输出子矩阵 for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { System.out.print("Submatrix (" + i + ", " + j + "): "); for (int p = 0; p < k; p++) { for (int q = 0; q < k; q++) { System.out.print(submatrices[i][j][p * k + q] + " "); } } System.out.println(); } } } } ``` 这段代码中,我们首先根据题目要求找到n的因子2的个数k,然后初始化两个n阶矩阵的元素。接着,我们定义一个三维数组submatrices来存储m*m个子矩阵,按照要求划分子矩阵,最后输出即可。注意,这里输出的子矩阵是按照题目要求展开的,即先输出第一行的所有元素,再输出第二行的所有元素,以此类推。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值