Where Will the Ball Fall
Try to solve the Where Will the Ball Fall problem.
We'll cover the following
Statement
You have balls and a 2D grid of size representing a box. The box is open on the top and bottom sides. Each cell in the box has a diagonal that can redirect a ball to the right or the left. You must drop balls at each column’s top. The goal is to determine whether each ball will fall out of the bottom or become stuck in the box. Each cell in the grid has a value of or .
- represents that the grid will redirect the ball to the right.
- represents that the grid will redirect the ball to the left.
A ball gets stuck if it hits a V-shaped pattern between two grids or if a grid redirects the ball into either wall of the box.
The solution should return an array of size , with the element indicating the column that the ball falls out of, or it becomes if it’s stuck. If the ball drops from column and falls out from column , then in the result array, index contains value .
Constraints:
-
grid.length
-
grid[i].length
grid[i][j]
is or .
Example
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy