Strange submit vs. testcase behavior


  • 0
    S

    I submitted and got a wrong answer. However, trying that same test case in the custom test case gives the expected answer. Here is a screenshot. Any ideas what I am doing wrong?

    0_1508497783187_Screen Shot 2017-10-20 at 4.37.16 PM.png

    And here is my code:

    class Solution(object):
        
        memos = {}
        
        def uniquePathsWithObstacles(self, obstacleGrid):
            """
            :type obstacleGrid: List[List[int]]
            :rtype: int
            """
            
            if len(obstacleGrid) == 0 or len(obstacleGrid[0]) == 0:
                return 0
            
            m = len(obstacleGrid)
            n = len(obstacleGrid[0])
            
            if obstacleGrid[0][0] == 1 or obstacleGrid[m-1][n-1] == 1:
                return 0
            
            return self.pathHelper(m-1, n-1, obstacleGrid)
            
            
        def pathHelper(self, m, n, obstacleGrid):            
            
            memo = self.memos.get((m, n), None)
            if memo:
                return memo
            
            if m == 0:
                if n == 0:
                    return 1
                if obstacleGrid[m][n-1] == 0:
                    return self.pathHelper(m, n-1, obstacleGrid)
                else:
                    return 0
            
            if n == 0:
                if m == 0:
                    return 1
                if obstacleGrid[m-1][n] == 0:
                    return self.pathHelper(m-1, n, obstacleGrid)
                else:
                    return 0
            
            a = 0
            b = 0
            
            if obstacleGrid[m-1][n] == 0:
                a = self.pathHelper(m-1, n, obstacleGrid)
                self.memos[(m-1, n)] = a
            if obstacleGrid[m][n-1] == 0:
                b = self.pathHelper(m, n-1, obstacleGrid)
                self.memos[(m, n-1)] = b
            
            return a + b
    

Log in to reply
 

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