# LeetCode: Where Will the Ball Fall Solution

If I remember correctly, Alex K. also loves recursion

## Approach

Keep in mind for the cases of:

• borders
• V shape

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var findBall = function (grid) {2  const [m, n] = [grid.length, grid[0].length]3
4  const recursion = (i, j) => {5    if (i === m) return j6
7    // the borders8    if (j === 0 && grid[i][j] === -1) return -19    if (j === n - 1 && grid[i][j] === 1) return -110
11    // the \/s12    if (grid[i][j] === 1 && grid[i][j + 1] === -1) return -113    if (grid[i][j] === -1 && grid[i][j - 1] === 1) return -114
15    return recursion(i + 1, j + grid[i][j])16  }17
18  return Array.from({ length: n }, (_, j) => recursion(0, j))19}```

Original problem

leetcode

array

matrix

recursion

dfs

## Next Post

LeetCode: Multiply Strings

Aug 28, 2022

Will make this better. Word promised!

## Previous Post

LeetCode: Spiral Matrix

Aug 25, 2022

Writing recursive function is fun

Search Posts