Skip to content

Practice Where Will the Ball Fall

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1706
LeetCode Problem

Where Will the Ball Fall

Problem Statement

Drop one ball from the top of each column of grid. A cell with 1 has a top-left to bottom-right board directing the ball right; -1 has the opposite slope directing it left. A ball gets stuck when directed into an outside wall or a V formed by opposing adjacent boards. Otherwise it continues down through the rows. Return each ball's exit column, or -1 if stuck, in starting-column order.

Source: Where Will the Ball Fall on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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]]
Output
[1,-1,-1,-1,-1]

# Example 2

Input
grid = [[-1]]
Output
[-1]

# Example 3

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]]
Output
[0,1,2,3,4,-1]

Constraints

m == grid.length
n == grid[i].length
1 <= m, n <= 100
grid[i][j] is 1 or -1.

Related LeetCode Problems

Practice more problems similar to Where Will the Ball Fall.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.