-
Notifications
You must be signed in to change notification settings - Fork 17
/
number-of-corner-rectangles.py
43 lines (34 loc) · 1.04 KB
/
number-of-corner-rectangles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution:
def countCornerRectangles(self, grid):
result = 0
row_cache = [
set([col for col, val in enumerate(row) if val])
for row in grid
]
for row in range(len(grid)):
for row_inner in row_cache[row + 1:]:
common = len(row_inner & row_cache[row])
result += common * (common - 1) // 2
return result
class TestSolution:
def setup(self):
self.sol = Solution()
def test_one(self):
assert self.sol.countCornerRectangles([[1]]) == 0
def test_custom1(self):
assert self.sol.countCornerRectangles(
[[1, 0, 0, 1, 0],
[0, 0, 1, 0, 1],
[0, 0, 0, 1, 0],
[1, 0, 1, 0, 1]]
) == 1
def test_custom2(self):
assert self.sol.countCornerRectangles(
[[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]
) == 9
def test_custom3(self):
assert self.sol.countCornerRectangles(
[[1, 1, 1, 1]]
) == 0