faster than 85%, In place, pyhton with explanation


  • 0
    A
    class Solution(object):
        def uniquePathsWithObstacles(self, m):
            r=len(m)
            if(len==0): return 0
            c=len(m[0])
            #if there is an obstacle at the start or ant end there are zero ways
            if(m[0][0] == 1 or m[r-1][c-1] == 1): return 0
            i=0
            m[0][0]=1
            while i<r:
                j=0
                while j<c:
                    #obstacle can be 1 except if it is at the first position
                    if(m[i][j]==1 and not (i==0 and j==0)):
                        #making it zero will result in ignoring this path while couting
                        m[i][j]=0
                        j+=1
                        continue
                    if(j!=0):
                        m[i][j]+=m[i][j-1]
                    if(i!=0):
                        m[i][j]+=m[i-1][j]
                    j+=1
                i+=1
            #last position will have our result
            return m[r-1][c-1]
                    
            """
            :type obstacleGrid: List[List[int]]
            :rtype: int
            """
            
    

Log in to reply
 

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