Maximum Trailing Zeros in a Cornered Path LeetCode solution in C++

1
array<int, 2> operator+(const array<int, 2> &l, const array<int, 2> &r) {
2
return {l[0] + r[0], l[1] + r[1]};
3
}
4
array<int, 2> operator-(const array<int, 2> &l, const array<int, 2> &r) {
5
return {l[0] - r[0], l[1] - r[1]};
6
}
7
int pairs(const array<int, 2> &p) {
8
return min(p[0], p[1]);
9
}
10

11
class Solution {
12
public:
13
int factors(int i, int f) {
14
return i % f ? 0 : 1 + factors(i / f, f);
15
}
16
int maxTrailingZeros(vector<vector<int>> &grid) {
17
int m = grid.size(), n = grid[0].size(), res = 0;
18
vector<vector<array<int, 2>>> h(m, vector<array<int, 2>>(n + 1)),
19
v(m + 1, vector<array<int, 2>>(n));
20
for (int i = 0; i < m; ++i)
21
for (int j = 0; j < n; ++j) {
22
array<int, 2> f25 = {factors(grid[i][j], 2), factors(grid[i][j], 5)};
23
v[i + 1][j] = v[i][j] + f25;
24
h[i][j + 1] = h[i][j] + f25;
25
}
26
for (int i = 0; i < m; ++i)
27
for (int j = 0; j < n; ++j) {
28
auto v1 = v[i + 1][j], v2 = v[m][j] - v[i][j];
29
auto h1 = h[i][j], h2 = h[i][n] - h[i][j + 1];
30
res = max({res, pairs(v1 + h1), pairs(v1 + h2), pairs(v2 + h1), pairs(v2 + h2)});
31
}
32
return res;
33
}
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0