Skip to content

Commit

Permalink
Time: 6 ms (70.73%), Space: 54.8 MB (63.01%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit-S-Sahu committed Oct 27, 2024
1 parent 23c9e5a commit 24b1a08
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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++) {
dp[i][0] = matrix[i][0];
ans += dp[i][0];
}

for (int j = 1; j < m; j++) {
dp[0][j] = matrix[0][j];
ans += dp[0][j];
}

for(int i = 1; i < n; i++) {
for(int j = 1; j < m; j++) {
if(matrix[i][j] == 1) dp[i][j] = 1 + Math.min(Math.min(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]);
ans += dp[i][j];
}
}
return ans;
}
}

0 comments on commit 24b1a08

Please sign in to comment.