Skip to content

Commit effb6df

Browse files
committed
added iterative CountSquareSubmatricesWithAllOnes solution (medium)
1 parent ee4a0b8 commit effb6df

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
# Count Square Submatrices with All Ones
3+
[Leetcode Link](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)
4+
5+
## Problem:
6+
7+
Given a `m * n` matrix of ones and zeros, return how many **square** submatrices have all ones.
8+
9+
## Example:
10+
11+
```
12+
Input: matrix =
13+
[
14+
[0,1,1,1],
15+
[1,1,1,1],
16+
[0,1,1,1]
17+
]
18+
Output: 15
19+
Explanation:
20+
There are 10 squares of side 1.
21+
There are 4 squares of side 2.
22+
There is 1 square of side 3.
23+
Total number of squares = 10 + 4 + 1 = 15.
24+
```
25+
26+
```
27+
Input: matrix =
28+
[
29+
[1,0,1],
30+
[1,1,0],
31+
[1,1,0]
32+
]
33+
Output: 7
34+
Explanation:
35+
There are 6 squares of side 1.
36+
There is 1 square of side 2.
37+
Total number of squares = 6 + 1 = 7.
38+
```
39+
40+
## Note:
41+
42+
- `1 <= arr.length <= 300`
43+
- `1 <= arr[0].length <= 300`
44+
- `0 <= arr[i][j] <= 1`
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import List
2+
3+
class Solution:
4+
def countSquares(self, matrix: List[List[int]]) -> int:
5+
count = 0
6+
# max side length is bound by the shortest side of original matrix
7+
maxSide = min(len(matrix), len(matrix[0]))
8+
# print(maxSide)
9+
# iterate over side length 1 to max side length, check if square is all ones (all ones if sum of elements = side**2)
10+
for side in range(1, maxSide+1):
11+
# print(f'======= side length of {side} ======')
12+
for i in range(0, len(matrix)-side+1):
13+
for j in range(0, len(matrix[i])-side+1):
14+
# print(f'{i}, {j}: {matrix[i][j]}')
15+
# print(f'sum of Square is: {self.sumOfSquare(matrix, i, j, side)}')
16+
if self.sumOfSquare(matrix, i, j, side) == side**2:
17+
count += 1
18+
return count
19+
20+
# helper function to sum up the elements in the square given left-upper corner and side length
21+
def sumOfSquare(self, matrix: List[List[int]], row: int, col: int, side: int) -> int:
22+
sum = 0
23+
for i in range(row, row+side):
24+
for j in range(col, col+side):
25+
sum += matrix[i][j]
26+
return sum
27+
28+
29+
sol = Solution()
30+
mat = [[0,1,1,1],
31+
[1,1,1,1],
32+
[0,1,1,1]]
33+
output = sol.countSquares(mat)
34+
print(output)
35+
36+
mat2 = [
37+
[1,0,1],
38+
[1,1,0],
39+
[1,1,0]
40+
]
41+
output2 = sol.countSquares(mat2)
42+
print(output2)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Languages used: Java and Python
7979
- [Sort Colors](Medium/SortColors)
8080
- [Design Linked List](Medium/DesignLinkedList)
8181
- [Single Number II](Medium/SingleNumber2)
82+
- [Count Square Submatrices with All Ones](Medium/CountSquareWithOnes)
8283
- Hard
8384
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)
8485
- [Reducing Dishes](Hard/ReducingDishes)

0 commit comments

Comments
 (0)