Test case fails (is something wrong?)


  • 0
    A

    Test case [[13,5,13,9],[5,0,2,9],[10,13,11,10],[0,0,13,13]] computes to 5 in my solution, which seems to be correct on manual examination. But the given solution is 6. Why?
    '''
    class Solution(object):
    def toposort(self, a, i, j, visited, p, m, n, t):
    visited[i][j]=t;
    p1,p2,p3,p4=0,0,0,0;
    if p[i][j]!=0:
    return p[i][j],p,visited;
    if i<m-1 and a[i+1][j]>a[i][j] and visited[i+1][j]!=t:
    p1,p,visited=self.toposort(a,i+1,j,visited,p,m,n,t);
    if j<n-1 and a[i][j+1]>a[i][j] and visited[i][j+1]!=t:
    p2,p,visited=self.toposort(a,i,j+1,visited,p,m,n,t);
    if i>0 and a[i-1][j]>a[i][j] and visited[i-1][j]!=t:
    p3,p,visited=self.toposort(a,i-1,j,visited,p,m,n,t);
    if j>0 and a[i][j-1]>a[i][j] and visited[i][j-1]!=t:
    p4,p,visited=self.toposort(a,i,j-1,visited,p,m,n,t);
    p[i][j]=1+max(p1,p2,p3,p4);
    return p[i][j],p,visited;
    def longestIncreasingPath(self, matrix):
    """
    :type matrix: List[List[int]]
    :rtype: int
    """
    m=len(matrix);
    if m==0:
    return 0;
    n=len(matrix[0]);
    p=[[0 for j in range(n)] for i in range(m)];
    visited=[[-1 for j in range(n)] for i in range(m)];
    maxval=0;
    for i in range(m):
    for j in range(n):
    t=n*i+j;
    val,p,visited=self.toposort(matrix,i,j,visited,p,m,n,t);
    if val>maxval:
    maxval=val;
    print(p);
    return maxval;
    '''


  • 0
    A

    Ah, visited matrix was causing the trouble. Remove it and boom! accepted


Log in to reply
 

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