LeetCode 1504. [Python] Count Submatrices With All Ones
原题链接
中等
作者:
徐辰潇
,
2020-07-08 08:45:37
,
所有人可见
,
阅读 1058
class Solution:
#TC: O(row*col*row)
#SC: O(row*col)
def numSubmat(self, mat: List[List[int]]) -> int:
nums = [[0]*len(mat[0]) for _ in range(len(mat))]
for row in range(len(mat)):
for col in range(len(mat[0])):
if col == 0:
nums[row][col] = mat[row][col]
else:
if mat[row][col] == 0:
nums[row][col] = 0
else:
nums[row][col] = nums[row][col-1] + 1
res = 0
for row in range(len(mat)):
for col in range(len(mat[0])):
Min = nums[row][col]
k = row
while(k>=0):
if nums[k][col] == 0:
break
Min = min(nums[k][col], Min)
res += Min
k -= 1
return res