Skip to content

Commit

Permalink
Create stamping-the-grid.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Jan 9, 2022
1 parent be82d7a commit c724d66
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Python/stamping-the-grid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Time: O(m * n)
# Space: O(m * n)

class Solution(object):
def possibleToStamp(self, grid, stampHeight, stampWidth):
"""
:type grid: List[List[int]]
:type stampHeight: int
:type stampWidth: int
:rtype: bool
"""
prefix = [[0]*(len(grid[0])+1) for _ in xrange(len(grid)+1)]
fit = [[0]*len(grid[0]) for _ in xrange(len(grid))]
for i in xrange(len(grid)):
for j in xrange(len(grid[0])):
prefix[i+1][j+1] = prefix[i+1][j]+prefix[i][j+1]-prefix[i][j]+(1^grid[i][j])
if i+1 >= stampHeight and j+1 >= stampWidth:
x, y = i+1-stampHeight, j+1-stampWidth
fit[i][j] = int(prefix[i+1][j+1]-prefix[x][j+1]-prefix[i+1][y]+prefix[x][y] == stampWidth*stampHeight)
prefix2 = [[0]*(len(grid[0])+1) for _ in xrange(len(grid)+1)]
for i in xrange(len(grid)):
for j in xrange(len(grid[0])):
prefix2[i+1][j+1] = prefix2[i+1][j]+prefix2[i][j+1]-prefix2[i][j]+fit[i][j]
for i in xrange(len(grid)):
for j in xrange(len(grid[0])):
x, y = min(i+stampHeight, len(grid)), min(j+stampWidth, len(grid[0]))
if not grid[i][j] and not prefix2[x][y]-prefix2[i][y]-prefix2[x][j]+prefix2[i][j]:
return False
return True

0 comments on commit c724d66

Please sign in to comment.