Python Solution commented
here is a simple explaination\n\'\'\'\n\n\tclass Solution:\n\t\tdef findBall(self, grid: List[List[int]]) -> List[int]:\n\t\t\tn=len(grid[0]) # no of cols\n\t\t
here is a simple explaination\n\'\'\'\n\n\tclass Solution:\n\t\tdef findBall(self, grid: List[List[int]]) -> List[int]:\n\t\t\tn=len(grid[0]) # no of cols\n\t\t\tm=len(grid) # no of rows\n\t\t\tres=[-1 for _ in range(n)] # initialise the result array as an array of -1 \n        \n\t\t\tfor a in range(n):\n\t\t\t\ti=a\n\t\t\t\tf=True  # we use f as flag variable, inital value is true but will bw changed to false if and when ball gets trapped \n\t\t\t\tfor j in range(m):\n\t\t\t\t\tif grid[j][i]==1:\n\t\t\t\t\t\tif i+1>=n: # this means that the ball has hit the right wall and will not make it through\n\t\t\t\t\t\t\tflag=False\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\tif i+1<n and grid[j][i+1]==-1: # this means that the ball is trapped in a pocket and won\'t make it through\n\t\t\t\t\t\t\tflag=False\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\ti+=1\n\t\t\t\t\telse:\n\t\t\t\t\t\tif i-1<0: # this means that the ball has hit the left wall and will not make it through\n\t\t\t\t\t\t\tflag=False\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\tif i-1>=0 and grid[j][i-1]==1:  # this means that the ball is trapped in a pocket and won\'t make it through\n\t\t\t\t\t\t\tflag=False\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\ti-=1\n\t\t\t\tif flag: # we check the flag variable f to see if the ball was trapped anywhere, true value indicates the ball will make it to the end \n\t\t\t\t\tres[a]=i\n\t\t\t\telse : res[a]=-1\n\t\t\treturn res\n\'\'\'
['Depth-First Search', 'Python']