Leetcode 1706: Where Will the Ball Fall

grid47
grid47
Exploring patterns and algorithms
May 20, 2024 7 min read

You have a 2-D grid representing a box, and a number of balls that will be dropped into the box. The box has diagonal boards in each cell, which can redirect the balls either left or right. Your task is to determine the path of each ball dropped from the top of the box. The ball can either fall out of the bottom, or get stuck if it hits a ‘V’ shaped pattern between two boards or is redirected into a wall.
Problem
Approach
Steps
Complexity
Input: You are given a 2-D grid of size 'm' x 'n' where each element in the grid can either be 1 or -1. A value of 1 indicates a board that redirects a ball to the right, and -1 indicates a board that redirects a ball to the left. Additionally, you are given an integer 'n' representing the number of balls.
Example: [[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]]
Constraints:
• 1 <= grid.length <= 100
• 1 <= grid[i].length <= 100
• grid[i][j] is either 1 or -1
• 1 <= n <= 100
Output: Return an array of size 'n' where the 'i'th element represents the column that the 'i'th ball falls out of at the bottom of the box, or -1 if the ball gets stuck.
Example: [1,-1,-1,-1,-1]
Constraints:
• The output array should contain integers between 0 and n-1, or -1 if a ball gets stuck.
Goal: Simulate the dropping of balls into the grid and track their paths based on the redirection rules.
Steps:
• 1. For each ball, start at the top of a given column and simulate its movement through the grid.
• 2. At each row, check the direction of the board in the current cell and determine if the ball can be redirected.
• 3. If the ball hits a wall or gets stuck in a 'V' shaped pattern, mark it as stuck and break out of the loop.
• 4. If the ball reaches the bottom without getting stuck, track the column where it exits.
Goal: Handle the problem within the constraints provided, ensuring each ball's path is computed accurately based on the grid configuration.
Steps:
• The grid is guaranteed to have at least one row and one column.
• Each ball can only fall down one path at a time and may get stuck or exit the grid.
Assumptions:
• Each ball will be dropped from the top of a column and can either exit the grid or get stuck.
Input: [[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]]
Explanation: Ball 0 is dropped at column 0 and falls out at column 1. Ball 1 is dropped at column 1 but gets stuck between columns 2 and 3. Balls 2-4 also get stuck due to the V-shaped pattern.

Input: [[-1]]
Explanation: Ball 0 is dropped at column 0 but gets stuck against the left wall, resulting in -1.

Input: [[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]]
Explanation: Each ball from columns 0-4 exits the box through the respective columns at the bottom. Ball 5 gets stuck.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus