Solution: Where Will the Ball Fall
Explore how to solve the ball drop problem by simulating ball movements through a grid with diagonal redirects. Understand the conditions that cause balls to fall out or get stuck, and implement an efficient algorithm to find each ball's final position. This lesson helps you apply matrix traversal patterns to coding interview problems.
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 ...