1706. Where Will the Ball Fall (Leetcode daily challenge Nov 1) dynamic programming
\'\'\'\nclass Solution {\npublic:\n    vector findBall(vector>& grid) {\n        const int m = grid.size();\n        const int n = grid[0].size();\n        vect
\'\'\'\nclass Solution {\npublic:\n    vector<int> findBall(vector<vector<int>>& grid) {\n        const int m = grid.size();\n        const int n = grid[0].size();\n        vector<int> dp(n);\n        vector<int> ans(n, -1);\n\n        iota(begin(dp), end(dp), 0);\n\n        for (int i = 0; i < m; ++i) {\n          vector<int> newDp(n, -1);\n          for (int j = 0; j < n; ++j) {\n            if (j + grid[i][j] < 0 || j + grid[i][j] == n)\n              continue;\n            if (grid[i][j] == 1 && grid[i][j + 1] == -1 ||\n                grid[i][j] == -1 && grid[i][j - 1] == 1)\n              continue;\n            newDp[j + grid[i][j]] = dp[j];\n          }\n          dp = move(newDp);\n        }\n\n        for (int i = 0; i < n; ++i)\n          if (dp[i] != -1)\n            ans[dp[i]] = i;\n\n        return ans;\n    }\n};\n\'\'\'
[]