Solution: Where Will the Ball Fall
Explore how to simulate ball movement through a 2D grid with diagonals guiding direction. This lesson teaches you to identify when balls get stuck or fall out, using an iterative approach with O(m×n) time complexity and constant space, enhancing your matrix traversal and problem-solving skills.
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 ...