Created
January 29, 2022 16:15
-
-
Save svaza/3f5a5381bbbd26c9cb469d93dbb26a94 to your computer and use it in GitHub Desktop.
1277. Count Square Submatrices with All Ones
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://leetcode.com/problems/count-square-submatrices-with-all-ones/ | |
| function countSquares(matrix: number[][]): number { | |
| let count = 0; | |
| for(let row = 0; row < matrix.length; row++) { | |
| for(let column = 0; column < matrix[row].length; column++) { | |
| let squareLength = 0; | |
| while(squareLength < matrix.length) { | |
| if(isOneMatrix(matrix, row, column, squareLength)) { | |
| count++; | |
| } | |
| squareLength++; | |
| } | |
| } | |
| } | |
| return count; | |
| }; | |
| function isOneMatrix(matrix: number[][], startRow: number, startColumn: number, squareLength: number): boolean { | |
| if(squareLength + startRow >= matrix.length) return false; | |
| else if(squareLength + startColumn >= matrix[startRow].length) return false; | |
| for(let i = startRow; i <= startRow + squareLength; i++) { | |
| for(let j = startColumn; j <= startColumn + squareLength; j++) { | |
| if(matrix[i][j] != 1) return false; | |
| } | |
| } | |
| return true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment