My code works while running on console but on submission doesn't gives required output.

image

You can see in the above image, the code is accepted while running in console while on submission the code gets rejected and the output gets changed even though the test case is same at both places, idk why this is happening.

I am not changing the code at all and yet recieving different outputs on console and on submission for the same testcase.

This is the link for the problem:-
https://leetcode.com/problems/shortest-path-visiting-all-nodes/

I am also sharing the code:-

class Solution(object):
    def shortestPathLength(self, graph, cur=0, l=[], ans=float("inf")):
        """
        :type graph: List[List[int]]
        :rtype: int
        """
        c=1
        for i in range(len(graph)):
            if i not in l:
                c=0
                break
        if( c or len(l) > ans ):
            # print(l)
            # print(ans)
            return(len(l) - 1)
        elif( len(l) == 0 ):
            for i in graph:
                for j in i:
                    l.append(j)
                    # print(l)
                    temp = self.shortestPathLength(graph, j, l, ans)
                    l=[]
                    if(ans > temp):
                        ans = temp
        else:
            for i in graph[cur]:
                if i not in l or graph[cur][len(graph[cur]) - 1] == i:
                    c= l[:]
                    c.append(i)
                    temp = self.shortestPathLength(graph, i, c, ans)
                    if(ans > temp):
                        ans = temp
                    
        return ans
            
Comments (2)