|
| 1 | +""" |
| 2 | +1706. Where Will the Ball Fall |
| 3 | +Medium |
| 4 | +Array | Dynamic Programming | Depth-First Search | Matrix | Simulation |
| 5 | +--- |
| 6 | +You have a 2-D grid of size m x n representing a box, and you have n balls. The box is open on the top and bottom sides. |
| 7 | +
|
| 8 | +Each cell in the box has a diagonal board spanning two corners of the cell that |
| 9 | + can redirect a ball to the right or to the left. |
| 10 | +
|
| 11 | +A board that redirects the ball to the right spans the top-left corner to |
| 12 | + the bottom-right corner and is represented in the grid as 1. |
| 13 | +A board that redirects the ball to the left spans the top-right corner to |
| 14 | + the bottom-left corner and is represented in the grid as -1. |
| 15 | +We drop one ball at the top of each column of the box. Each ball can get stuck in the box or fall out of the bottom. |
| 16 | + A ball gets stuck if it hits a "V" shaped pattern between two boards or |
| 17 | + if a board redirects the ball into either wall of the box. |
| 18 | +
|
| 19 | +Return an array answer of size n where answer[i] is the column that the ball falls out of at |
| 20 | + the bottom after dropping the ball from the ith column at the top, or -1 if the ball gets stuck in the box. |
| 21 | +
|
| 22 | +Example 1: |
| 23 | +Input: grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]] |
| 24 | +Output: [1,-1,-1,-1,-1] |
| 25 | +Explanation: This example is shown in the photo. |
| 26 | +Ball b0 is dropped at column 0 and falls out of the box at column 1. |
| 27 | +Ball b1 is dropped at column 1 and will get stuck in the box between column 2 and 3 and row 1. |
| 28 | +Ball b2 is dropped at column 2 and will get stuck on the box between column 2 and 3 and row 0. |
| 29 | +Ball b3 is dropped at column 3 and will get stuck on the box between column 2 and 3 and row 0. |
| 30 | +Ball b4 is dropped at column 4 and will get stuck on the box between column 2 and 3 and row 1. |
| 31 | +
|
| 32 | +Example 2: |
| 33 | +Input: grid = [[-1]] |
| 34 | +Output: [-1] |
| 35 | +Explanation: The ball gets stuck against the left wall. |
| 36 | +
|
| 37 | +Example 3: |
| 38 | +Input: grid = [[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]] |
| 39 | +Output: [0,1,2,3,4,-1] |
| 40 | +
|
| 41 | +Constraints: |
| 42 | +m == grid.length |
| 43 | +n == grid[i].length |
| 44 | +1 <= m, n <= 100 |
| 45 | +grid[i][j] is 1 or -1. |
| 46 | +""" |
| 47 | + |
| 48 | + |
| 49 | +from typing import List |
| 50 | + |
| 51 | + |
| 52 | +# O(m * n) time | O(m * n) space |
| 53 | +class Solution: |
| 54 | + def findBall(self, grid: List[List[int]]) -> List[int]: |
| 55 | + def dfs(row, col, grid): |
| 56 | + if row == len(grid): |
| 57 | + return col |
| 58 | + nextCol = col + grid[row][col] |
| 59 | + if nextCol < 0 or nextCol > len(grid[0]) - 1 or grid[row][col] != grid[row][nextCol]: |
| 60 | + return -1 |
| 61 | + return dfs(row + 1, nextCol, grid) |
| 62 | + |
| 63 | + res = [] |
| 64 | + for i in range(len(grid[0])): |
| 65 | + res.append(dfs(0, i, grid)) |
| 66 | + return res |
0 commit comments