(C++) 2245. Maximum Trailing Zeros in a Cornered Path
\n\nclass Solution {\npublic:\n    int maxTrailingZeros(vector<vector<int>>& grid) {\n        int m = grid.size(), n = grid[0].size(); \n        vector<vector<i
\n```\nclass Solution {\npublic:\n    int maxTrailingZeros(vector<vector<int>>& grid) {\n        int m = grid.size(), n = grid[0].size(); \n        vector<vector<int>> f2(m, vector<int>(n)), f5(m, vector<int>(n)); \n        for (int i = 0; i < m; ++i) \n            for (int j = 0; j < n; ++j) {\n                for (int x = grid[i][j]; x % 2 == 0; x /= 2) ++f2[i][j]; \n                for (int x = grid[i][j]; x % 5 == 0; x /= 5) ++f5[i][j]; \n            }\n        vector<vector<array<int, 2>>> h(m+1, vector<array<int, 2>>(n+1)), v = h;\n        for (int i = 0; i < m; ++i) \n            for (int j = 0; j < n; ++j) {\n                h[i][j+1][0] = h[i][j][0] + f2[i][j]; \n                h[i][j+1][1] = h[i][j][1] + f5[i][j]; \n                v[i+1][j][0] = v[i][j][0] + f2[i][j]; \n                v[i+1][j][1] = v[i][j][1] + f5[i][j]; \n            }\n        int ans = 0; \n        for (int i = 0; i < m; ++i) {\n            for (int j = 0; j < n; ++j) {\n                vector<int> hh = {h[i][n][0] - h[i][j][0], h[i][n][1] - h[i][j][1]}, vv = {v[m][j][0] - v[i][j][0], v[m][j][1] - v[i][j][1]}; \n                ans = max(ans, min(h[i][j][0]+v[i][j][0]+f2[i][j], h[i][j][1]+v[i][j][1]+f5[i][j])); \n                ans = max(ans, min(hh[0]+v[i][j][0], hh[1]+v[i][j][1])); \n                ans = max(ans, min(h[i][j][0]+vv[0], h[i][j][1]+vv[1])); \n                ans = max(ans, min(hh[0]+vv[0]-f2[i][j], hh[1]+vv[1]-f5[i][j])); \n            }\n        }\n        return ans; \n    }\n};\n```
['C']