Why the codes behaves different between case-test and "Run Code" Option for the same case?


  • 0
    Z

    The submission result shows that my codes return a 4 when given case "[ ]", but it returned 0 on my mac. When I customized the "Run Code" option to set the test case to be "[ ]", it also works well.

    Here is my codes:
    '''
    UP = 1
    DOWN = 2
    LEFT = 3
    RIGHT = 4

    class Solution(object):
        direction_catches = []
        result = []
    
        def longestIncreasingPath(self, matrix):
            self.direction_catches = [[[-1, -1] for i in range(len(matrix[0]))] for i in range(len(matrix))]
    
            for i in range(len(matrix)):
                for j in range(len(matrix[0])):
                    self.result.append(self.seek_after(i, j, matrix))
            print(self.result)
            r = max(self.result) if len(self.result) else 0
            print(r)
            return r
    
        def seek_after(self, i, j, matrix):
            if self.direction_catches[i][j][1] != -1:
                return self.direction_catches[i][j][1]
    
            up = [UP, 1]
            down = [DOWN, 1]
            left = [LEFT, 1]
            right = [RIGHT, 1]
    
            if i != 0 and matrix[i][j] < matrix[i - 1][j]:
                up[1] = self.seek_after(i - 1, j, matrix) + 1
            if i != len(matrix) - 1 and matrix[i][j] < matrix[i + 1][j]:
                down[1] = self.seek_after(i + 1, j, matrix) + 1
            if j != 0 and matrix[i][j] < matrix[i][j - 1]:
                left[1] = self.seek_after(i, j - 1, matrix) + 1
            if j != len(matrix[0]) - 1 and matrix[i][j] < matrix[i][j + 1]:
                right[1] = self.seek_after(i, j + 1, matrix) + 1
    
            remaining_length = [up, down, left, right]
            remaining_length = sorted(remaining_length, key=lambda x: x[1])
            self.direction_catches[i][j] = remaining_length[-1]
    
            return self.direction_catches[i][j][1]
    

    ''''


Log in to reply
 

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