Skip to content

Instantly share code, notes, and snippets.

@jeffchiucp
Created September 15, 2019 22:30
Show Gist options
  • Save jeffchiucp/7ed0dc7d1d300ad482cf05fbff6661fb to your computer and use it in GitHub Desktop.
Save jeffchiucp/7ed0dc7d1d300ad482cf05fbff6661fb to your computer and use it in GitHub Desktop.
updateBoard.py
class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
self.board = board
def dfs(i, j):
if i < 0 or j < 0 or i == len(board) or j == len(board[i]):
return
if self.board[i][j] not in ['E', 'M']:
return
if self.board[i][j] == 'M':
self.board[i][j] = 'X'
return
else:
cnt = 0
direct = [[1,0],[1,-1],[1,1],[-1,1],[-1,0],[-1,-1],[0,1],[0,-1]]
for d in direct:
cnt += detect(i+d[0], j+d[1])
if cnt == 0:
self.board[i][j] = 'B'
for d in direct:
dfs(i+d[0], j+d[1])
else:
self.board[i][j] = str(cnt)
def detect(i, j):
if i < 0 or j < 0 or i == len(board) or j == len(board[i]):
return 0
if self.board[i][j] in ['X','M']:
return 1
return 0
dfs(click[0], click[1])
return self.board
"""
Time Complexity: O(n)
Space Complexity: O(n)
n: size of the board
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment