-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
count-submatrices-with-equal-frequency-of-x-and-y.py
48 lines (45 loc) · 1.61 KB
/
count-submatrices-with-equal-frequency-of-x-and-y.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
44
45
46
47
48
# Time: O(n * m)
# Space: O(n * m)
# dp
class Solution(object):
def numberOfSubmatrices(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
result = 0
dp1 = [[0]*(len(grid[0])+1) for _ in xrange(len(grid)+1)]
dp2 = [[0]*(len(grid[0])+1) for _ in xrange(len(grid)+1)]
for i in xrange(len(grid)):
for j in xrange(len(grid[0])):
dp1[i+1][j+1] = dp1[i][j+1]+dp1[i+1][j]-dp1[i][j]+int(grid[i][j] == 'X')
dp2[i+1][j+1] = dp2[i][j+1]+dp2[i+1][j]-dp2[i][j]+int(grid[i][j] == 'Y')
result += int(dp1[i+1][j+1] == dp2[i+1][j+1] != 0)
return result
# Time: O(n * m)
# Space: O(n * m)
# dp
class Solution2(object):
def numberOfSubmatrices(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
result = 0
dp1 = [[0]*len(grid[0]) for _ in xrange(len(grid))]
dp2 = [[0]*len(grid[0]) for _ in xrange(len(grid))]
for i in xrange(len(grid)):
for j in xrange(len(grid[0])):
if i-1 >= 0:
dp1[i][j] += dp1[i-1][j]
dp2[i][j] += dp2[i-1][j]
if j-1 >= 0:
dp1[i][j] += dp1[i][j-1]
dp2[i][j] += dp2[i][j-1]
if i-1 >= 0 and j-1 >= 0:
dp1[i][j] -= dp1[i-1][j-1]
dp2[i][j] -= dp2[i-1][j-1]
dp1[i][j] += int(grid[i][j] == 'X')
dp2[i][j] += int(grid[i][j] == 'Y')
result += int(dp1[i][j] == dp2[i][j] != 0)
return result