class Solution:
def findBall(self, grid):
m, n = len(grid), len(grid[0])
res = []
for col in range(n):
curr_col = col
for row in range(m):
dir = grid[row][curr_col]
next_col = curr_col + dir
# Check if next_col is out of bounds or forms a V shape
if next_col < 0 or next_col >= n or grid[row][next_col] != dir:
curr_col = -1
break
curr_col = next_col
res.append(curr_col)
return res
class Solution {
public:
vector<int> findBall(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<int> res(n, 0);
for (int col = 0; col < n; ++col) {
int curr_col = col;
for (int row = 0; row < m; ++row) {
int dir = grid[row][curr_col];
int next_col = curr_col + dir;
if (next_col < 0 || next_col >= n || grid[row][next_col] != dir) {
curr_col = -1;
break;
}
curr_col = next_col;
}
res[col] = curr_col;
}
return res;
}
};
class Solution {
public int[] findBall(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] res = new int[n];
for (int col = 0; col < n; col++) {
int curr_col = col;
for (int row = 0; row < m; row++) {
int dir = grid[row][curr_col];
int next_col = curr_col + dir;
if (next_col < 0 || next_col >= n || grid[row][next_col] != dir) {
curr_col = -1;
break;
}
curr_col = next_col;
}
res[col] = curr_col;
}
return res;
}
}
var findBall = function(grid) {
let m = grid.length, n = grid[0].length;
let res = [];
for (let col = 0; col < n; col++) {
let curr_col = col;
for (let row = 0; row < m; row++) {
let dir = grid[row][curr_col];
let next_col = curr_col + dir;
if (next_col < 0 || next_col >= n || grid[row][next_col] != dir) {
curr_col = -1;
break;
}
curr_col = next_col;
}
res.push(curr_col);
}
return res;
};
You are given a 2D grid representing a box with m
rows and n
columns. Each cell in the grid contains either 1
or -1
:
1
means the cell's board slopes to the right-1
means the cell's board slopes to the left1
or left if it hits a -1
. However, if the ball hits the wall or gets stuck in a "V" shape (two adjacent boards sloping towards each other), it will get stuck and not fall out.
i
-th element is the column index where the ball dropped in column i
will fall out at the bottom, or -1
if it gets stuck.
To solve this problem, let's first imagine dropping a ball from each column and simulating its journey through the grid. At each step, the ball moves according to the slope in the current cell:
1
, the ball tries to move right.-1
, the ball tries to move left.The solution involves simulating the path of each ball as it moves down the grid. Here is a step-by-step breakdown:
1
or -1
).curr_col + dir
).-1
for that ball.
Let's consider the following grid as an example:
grid = [ [1, 1, -1, -1], [-1, -1, 1, 1], [1, 1, 1, -1] ]Let's trace the ball dropped at column 0:
1
(go right to col 1)-1
(go left to col 0)-1
, which matches direction, so continue1
(go right to col 1)1
, direction matches, so continue-1
(go left to col 1)1
, which does not match direction, so it's a "V" shape and the ball gets stuck-1
for this ballBrute-force Approach:
The key to this problem is simulating the path of each ball as it moves through the grid, checking for stuck conditions at each step. By carefully handling edge cases (walls and "V" shapes), we can efficiently determine where each ball will end up. The solution is elegant because it leverages the independence of each ball's journey and uses straightforward iteration for clarity and efficiency.