JS fast simple iterative solution
Runtime74 ms\nBeats\n92.79%\nMemory44.2 MB\nBeats\n87.18%\n\nThis approach simply follows the ball down from the top for each starting position tried, modifying
Runtime74 ms\nBeats\n92.79%\nMemory44.2 MB\nBeats\n87.18%\n\nThis approach simply follows the ball down from the top for each starting position tried, modifying the column as necessary.\n### Complexity\n- Time complexity: O(mn)\n- Space complexity: O(1)\n\n### Code\n```\n/**\n * @param {number[][]} grid\n * @return {number[]}\n */\nvar findBall = function(grid) \n{\n    const rows = grid.length, cols = grid[0].length;\n\n    const getPosition = function(row, col)\n    {\n        if(grid[row][col] === 1)\n        {\n            if(col === cols-1 || grid[row][col+1] === -1)\n                return -1;\n            return col+1;\n        }\n        else\n        {\n            if(col === 0 || grid[row][col-1] === 1)\n                return -1;\n            return col-1;\n        }\n    };\n\n    let result = [];\n    for(let col=0; col<cols; col++)\n    {\n        let pos = col;\n        for(let row=0; row<rows; row++)\n        {\n            pos = getPosition(row, pos);\n            if(pos === -1)\n                break;\n        }\n        result.push(pos);\n    }\n    \n    return result;\n};\n```
['JavaScript']