## Similar Problems

Similar Problems not available

# Where Will The Ball Fall - Leetcode Solution

## Companies:

LeetCode: Where Will The Ball Fall Leetcode Solution

Difficulty: Medium

Topics: matrix array simulation

Problem Statement:

Given a two-dimensional matrix representing the height of each cell, and a ball that is dropped at any cell of the matrix, the ball moves in the direction of the steepest descent until it hits a V-shaped barrier between two cells, and then bounces off the barrier in the opposite direction of the incoming direction. The gravity effect makes the ball move down the matrix cells towards the V-shaped barrier. The goal is to find the cell where the ball will stop.

Solution:

To solve this problem, we need to simulate the movement of the ball down the matrix cells. The key to simulating the ball movement is to find the direction of the steepest descent from the current cell and move the ball in that direction. We also need to check if the ball hits a V-shaped barrier, and if it hits, we need to bounce it off in the opposite direction.

We can simulate the ball movement using a while loop. We start with the cell where the ball is dropped, and we keep moving the ball in the direction of the steepest descent until it hits a V-shaped barrier.

To find the direction of the steepest descent, we need to check the neighboring cells of the current cell and find the cell with the minimum height. We can use the following code to find the minimum height cell:

```
int[] dir = {-1, 0, 1, 0, -1};
int min_row = row, min_col = col, min_height = heights[row][col];
for (int k = 0; k < 4; k++) {
int new_row = row + dir[k], new_col = col + dir[k+1];
if (new_row < 0 || new_row >= m || new_col < 0 || new_col >= n) continue;
if (heights[new_row][new_col] < min_height) {
min_row = new_row;
min_col = new_col;
min_height = heights[new_row][new_col];
}
}
```

The `dir`

array represents the four neighboring cells of the current cell. We iterate over the neighbors and check if the height of the neighbor is less than the current minimum height. If it is, we update the minimum height cell and continue the loop.

Once we find the minimum height cell, we need to check if the ball will hit a V-shaped barrier if we move it to the minimum height cell. A V-shaped barrier is a situation where the two cells adjacent to the ball are higher than the ball's current position. We can use the following code to check if the ball will hit a V-shaped barrier:

```
if (heights[min_row][min_col] >= heights[row][col] &&
((min_row == row - 1 && heights[min_row][min_col] >= heights[row][col-1]) ||
(min_row == row + 1 && heights[min_row][min_col] >= heights[row][col+1]) ||
(min_col == col - 1 && heights[min_row][min_col] >= heights[row-1][col]) ||
(min_col == col + 1 && heights[min_row][min_col] >= heights[row+1][col]))) {
// hit a V-shaped barrier
} else {
row = min_row;
col = min_col;
}
```

First, we check if the minimum height cell is higher than or equal to the current cell. If it is, we need to check if it is a V-shaped barrier. We check if the minimum height cell is adjacent to the current cell in one of the four directions. If it is, we need to check if the two cells adjacent to the ball are higher than the current position. If they are, we have hit a V-shaped barrier.

If we hit a V-shaped barrier, we need to bounce the ball off in the opposite direction. We can use the following code to do this:

```
if (min_row == row - 1 && heights[min_row][min_col] >= heights[row][col-1]) {
row = row - 1;
col = col - 1;
} else if (min_row == row + 1 && heights[min_row][min_col] >= heights[row][col+1]) {
row = row + 1;
col = col + 1;
} else if (min_col == col - 1 && heights[min_row][min_col] >= heights[row-1][col]) {
row = row - 1;
col = col + 1;
} else if (min_col == col + 1 && heights[min_row][min_col] >= heights[row+1][col]) {
row = row + 1;
col = col - 1;
}
```

We check which direction the ball was moving before hitting the V-shaped barrier, and we move it in the opposite direction.

We continue this process of finding the steepest descent direction, checking for V-shaped barriers, and bouncing off the ball until we find the cell where the ball will stop.

The complete solution for this problem can be found below:

```
class Solution {
public int[] findBall(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
int row = 0, col = i;
while (row < m) {
int[] dir = {-1, 0, 1, 0, -1};
int min_row = row, min_col = col, min_height = grid[row][col];
for (int k = 0; k < 4; k++) {
int new_row = row + dir[k], new_col = col + dir[k+1];
if (new_row < 0 || new_row >= m || new_col < 0 || new_col >= n) continue;
if (grid[new_row][new_col] < min_height) {
min_row = new_row;
min_col = new_col;
min_height = grid[new_row][new_col];
}
}
if (grid[min_row][min_col] >= grid[row][col] &&
((min_row == row - 1 && grid[min_row][min_col] >= grid[row][col-1]) ||
(min_row == row + 1 && grid[min_row][min_col] >= grid[row][col+1]) ||
(min_col == col - 1 && grid[min_row][min_col] >= grid[row-1][col]) ||
(min_col == col + 1 && grid[min_row][min_col] >= grid[row+1][col]))) {
// hit a V-shaped barrier
ans[i] = -1;
break;
} else {
row = min_row;
col = min_col;
}
if (row == m) {
ans[i] = col;
}
}
}
return ans;
}
}
```

In this solution, we iterate over each column of the matrix and simulate the ball movement starting from each cell in the first row. If we find a cell where the ball stops, we store the column index in the `ans`

array, and if the ball hits a V-shaped barrier, we store `-1`

in the `ans`

array.

## Where Will The Ball Fall Solution Code

`1`