"runtime error" for an easy understanding solution


  • 0
    G

    I have an easy understanding solution as following. But I got "runtime error" when the input is a large board.
    Could someone point out which part of my solution is wrong?

    class Solution(object):
        def solve(self, board):
            """
            :type board: List[List[str]]
            :rtype: void Do not return anything, modify board in-place instead.
            """
            m = len(board)
            if(m == 0): return 
            n = len(board[0])
            tmp = [ [char for char in row]  for row in board ]
            
            def dfs(x,y):
                if(x < 0 or x > m-1): return 
                if(y < 0 or y > n-1): return
                if(tmp[x][y] != "O"): return
                tmp[x][y] = "Y"
                dfs(x+1,y);
                dfs(x-1,y);
                dfs(x,y+1)
                dfs(x,y-1)
            
            #handle first and last rows
            for i in range(n):
                if(tmp[0][i] == "O"): 
                    tmp[0][i] = "Y"
                    dfs(1,i)
                    dfs(0,i+1)
                    dfs(0,i-1)
                if(tmp[m-1][i] == "O"):
                    tmp[m-1][i] = "Y"
                    dfs(m-2,i)
                    dfs(m-1,i+1)
                    dfs(m-1,i-1)
            #handle first and last columns
            for i in range(1,m-1):
                if(tmp[i][0] == "O"):
                    tmp[i][0] = "Y"
                    dfs(i-1,0)
                    dfs(i+1,0)
                    dfs(i,1)
                if(tmp[i][n-1] == "O"):
                    tmp[i][n-1] = "Y"
                    dfs(i-1,n-1)
                    dfs(i+1,n-1)
                    dfs(i,n-2)
            
            for i in range(m):
                for j in range(n):
                    if(tmp[i][j] == "Y"): tmp[i][j] = "O"
                    elif(tmp[i][j] == "O"): tmp[i][j] = "X"
            
            for i in range(m):
                board[i] = "".join(tmp[i])
            
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.