Skip to content

Instantly share code, notes, and snippets.

@oisincar
Created April 8, 2020 15:48
Show Gist options
  • Save oisincar/31c4623c19a27689ed56487daa78d480 to your computer and use it in GitHub Desktop.
Save oisincar/31c4623c19a27689ed56487daa78d480 to your computer and use it in GitHub Desktop.
class Solution:
def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
dimX, dimY = len(matrix), len(matrix[0])
for i in range(dimX+dimY):
#print("mat", matrix)
for x in range(dimX):
for y in range(dimY):
if matrix[x][y] == 0:
continue
ofX, ofY = [0,0,-1,1], [-1,1,0,0]
neighbours = [matrix[x+ofX[j]][y+ofY[j]] for j in range(4) if
x+ofX[j] >= 0 and x+ofX[j] < dimX and
y+ofY[j] >= 0 and y+ofY[j] < dimY]
#print(neighbours)
matrix[x][y] = 1+min(neighbours)
return matrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment