Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#Flood Fill

# Time -> O(mXn)
# Space -> O(mXn) -> Stack space
# Logic -> On the

class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:

oldColor = image[sr][sc]
newColor = color
if oldColor == newColor:
return image

dirs = [(-1,0), (0,1), (1,0), (0,-1)]

self.dfs(image, sr, sc, oldColor, newColor, dirs)

return image

def dfs(self, image, i, j, oldColor, newColor, dirs):

if i<0 or j< 0 or i==len(image) or j==len(image[0]) or image[i][j]!=oldColor:
return

image[i][j] = newColor
for r, c in dirs:
self.dfs(image, r+i, c+j, oldColor, newColor, dirs)

return
71 changes: 71 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# 01 Matrix

class Solution:
def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
rows = len(mat)
columns = len(mat[0])

result = [[0 for j in range(columns)] for i in range(rows)]

for i in range(rows):
for j in range(columns):
if mat[i][j] == 1:
result[i][j] = self.dfs(mat, result, i, j)


return result


def dfs(self, mat, res, row, column):

#base case

#top
if row>0 and mat[row-1][column] == 0:
return 1
#right
if column<(len(mat[0])-1) and mat[row][column+1] == 0:
return 1
#bottom
if row<len(mat)-1 and mat[row+1][column] == 0:
return 1
#left
if column>0 and mat[row][column-1] == 0:
return 1

top = 10001
right = 10001
bottom = 10001
left = 10001

#top
if row>0 and res[row-1][column] != 0:
top = res[row-1][column]

if column>0 and res[row][column-1] != 0:
left = res[row][column-1]

if column<(len(mat[0])-1):
if res[row][column+1] == 0:
res[row][column+1] = self.dfs(mat, res, row, column+1)
right = res[row][column+1]

if row<len(mat)-1:
if res[row+1][column] == 0:
res[row+1][column] = self.dfs(mat, res, row+1, column)
bottom = res[row+1][column]

return 1 + min(top, right, bottom, left)


# if i < 0 or j<0 or i == rows or j == columns or mat[i][j] == 1:
# return

# result[i][j] = min(result[i][j],depth)

# for r, c in dirs:
# if i < 0 or j<0 or i == rows or j == columns or mat[r][c] == 1:
# continue
# self.dfs(mat, r+i, j+c, result, dirs, depth+1, rows, columns)

# return