System judge to be wrong answer of python code

  • 0

    hi,bellow is my python code, which was judge for 'wrong answer' by system while work fine on my dev PC.

    class Solution:
        # @param grid, a list of lists of integers
        # @return an integer
        def minPathSum(self, grid):
            if not grid or len(grid) == 0 or len(grid[0]) == 0:
                return 0
            m, n = len(grid), len(grid[0])
            return minSum(grid, m - 1, n - 1)
    cache = {}
    def minSum(grid_data, m, n):
        # cache the result
        key = '%d_%d' % (m, n)
        if key in cache:
            return cache[key]
            #print key
        val = 0
        if m == 0 and n == 0:
            val = grid_data[0][0]
        elif m == 0:
            val = minSum(grid_data, m, n - 1) + grid_data[m][n]
        elif n == 0:
            val = minSum(grid_data, m - 1, n) + grid_data[m][n]
            val = min(minSum(grid_data, m - 1, n), minSum(grid_data, m, n - 1)) + grid_data[m][n]
        cache[key] = val
        return val

    After I change the minSum fucntion to class function like this submission, I got a accept. So I wonder if the global function is not allowed in the answer?

  • 0

    The reason is not in the function, it is because you declared cache as a global variable. The judger runs all test cases in one go, therefore you have to reset the variable in your function if you are using it as global.

  • 0

    Thx @1337c0d3r :)

    btw, I guest this may be a common mistake newbie like me will make, can we add some explain to FAQ?

Log in to reply

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