Skip to content

Latest commit

 

History

History
24 lines (22 loc) · 615 Bytes

1706.md

File metadata and controls

24 lines (22 loc) · 615 Bytes

1706. Where Will the Ball Fall

Solution 1 (time O(mn), space O(n))

class Solution(object):
    def findBall(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: List[int]
        """
        n = len(grid[0])
        ans = [-1] * n
        for j in range(n):
            col = j
            for row in grid:
                cur_direction = row[col]
                col += cur_direction
                if col < 0 or col >= n or row[col] != cur_direction:
                    break
            else:
                ans[j] = col
        return ans