Simple Solution with Diagrams in Video - JavaScript, C++, Java, Python
Video\nPlease upvote here so others save time too!\n\nLike the video on YouTube if you found it useful\n\nClick here to subscribe on YouTube:\nhttps://www.youtu
# Video\nPlease upvote here so others save time too!\n\nLike the video on YouTube if you found it useful\n\nClick here to subscribe on YouTube:\nhttps://www.youtube.com/@mayowadan?sub_confirmation=1\n\nThanks!\n\nhttps://youtu.be/8x572DnPAxk?si=hdZosG6hAzDCbs7p\n\n```Javascript []\n/**\n * @param {number[][]} grid\n * @return {number[]}\n */\nvar findBall = function (grid) {\n     // Initialize a result array with -1 to store the\n  // results\n  const result = Array(grid[0].length).fill(-1);\n\n  // Loop through each column in the grid\n  for (let col = 0; col < grid[0].length; col++) {\n    let currentColumn = col;\n\n    // Loop through each row in the grid\n    for (let row = 0; row < grid.length; row++) {\n      let nextColumn =\n        currentColumn + grid[row][currentColumn];\n\n      // Check if the ball is out of the grid or\n      // hit a "V" shaped pattern\n      if (\n        nextColumn < 0 ||\n        nextColumn > grid[0].length - 1 ||\n        grid[row][currentColumn] !== grid[row][nextColumn]\n      ) {\n        break;\n      }\n\n      // Check if the ball has reached the bottom\n      if (row === grid.length - 1) {\n        result[col] = nextColumn;\n      }\n      currentColumn = nextColumn;\n    }\n  }\n  return result;\n};\n\n```\n```Python []\nclass Solution:\n    def findBall(self, grid: List[List[int]]) -> List[int]:\n        result = [-1] * len(grid[0])\n\n        # Loop through each column in the grid\n        for col in range(len(grid[0])):\n            # Start the ball at the top of the current column\n            current_col = col\n            # Loop through each row in the grid\n            for row in range(len(grid)):\n                # Get the value of the diagonal grid at the current position\n                next_col = current_col + grid[row][current_col]\n                # Check if the ball is out of the grid\n                # Or hit a "V" shaped pattern\n                if (\n                    next_col < 0\n                    or next_col > len(grid[0]) - 1\n                    or grid[row][current_col] != grid[row][next_col]\n                ):\n                    break\n                # Check if the ball has reached the bottom\n                if row == len(grid) - 1:\n                    result[col] = next_col\n                current_col = next_col\n        return result\n\n```\n```Java []\nclass Solution {\n    public int[] findBall(int[][] grid) {\n        int[] result = new int[grid[0].length];\n\n        // Fill the result array with -1\n        for (int i = 0; i < result.length; i++) {\n            result[i] = -1;\n        }\n\n        // Loop through each column in the grid\n        for (int col = 0; col < grid[0].length; col++) {\n            int currentColumn = col;\n\n            // Loop through each row in the grid\n            for (int row = 0; row < grid.length; row++) {\n                int nextColumn = currentColumn + grid[row][currentColumn];\n\n                // Check if the ball is out of the grid\n                // or hit a "V" shaped pattern\n                if (nextColumn < 0 || nextColumn > grid[0].length - 1\n                        || grid[row][currentColumn] != grid[row][nextColumn]) {\n                    break;\n                }\n\n                // Check if the ball has reached the bottom\n                if (row == grid.length - 1) {\n                    result[col] = nextColumn;\n                }\n                currentColumn = nextColumn;\n            }\n        }\n\n        return result;\n    }\n}\n\n```\n```C++ []\nclass Solution {\npublic:\n    vector<int> findBall(vector<vector<int>>& grid) {\n        std::vector<int> result(grid[0].size(), -1);\n        // Loop through each column in the grid\n        for (int col = 0; col < grid[0].size(); col++) {\n            // Start the ball at the top of the current column\n            int currentColumn = col;\n            // Loop through each row in the grid\n            for (int row = 0; row < grid.size(); row++) {\n                // Get the value of the diagonal grid at the current position\n                int nextColumn = currentColumn + grid[row][currentColumn];\n                // Check if the ball is out of the grid\n                // or hit a "V" shaped pattern\n                if (nextColumn < 0 || nextColumn > grid[0].size() - 1 ||\n                    grid[row][currentColumn] != grid[row][nextColumn]) {\n                    break;\n                }\n                // Check if the ball has reached the bottom\n                if (row == grid.size() - 1) {\n                    result[col] = nextColumn;\n                }\n                currentColumn = nextColumn;\n            }\n        }\n        return result;\n    }\n};\n```
['Python', 'C++', 'Java', 'Python3', 'JavaScript']