1
var maxTrailingZeros = function (grid) {3
const n = grid[0].length;5
const postfixCols = [];7
for (let i = 0; i < m; ++i) {8
for (let j = 0; j < n; ++j) {9
const num = grid[i][j];11
if (postfixCols[j] == null) postfixCols[j] = { 2: 0, 5: 0 };13
postfixCols[j]["2"] += getCount(num, 2);14
postfixCols[j]["5"] += getCount(num, 5);18
const prefixCols = [];20
for (let j = 0; j < n; ++j) {21
prefixCols[j] = { 0: 0, 2: 0, 5: 0 };26
for (let i = 0; i < m; ++i) {27
const postfixRow = { 0: 0, 2: 0, 5: 0 };29
for (let j = n - 1; j >= 0; --j) {30
const num = grid[i][j];32
postfixRow["2"] += getCount(num, 2);33
postfixRow["5"] += getCount(num, 5);36
let prefixRow = { 0: 0, 2: 0, 5: 0 };38
for (let j = 0; j < n; ++j) {39
const num = grid[i][j];41
const twoCount = getCount(num, 2);42
const fiveCount = getCount(num, 5);44
postfixRow["2"] -= twoCount;45
postfixCols[j]["2"] -= twoCount;47
postfixRow["5"] -= fiveCount;48
postfixCols[j]["5"] -= fiveCount;50
// down-right => prefixCol + postfixRow51
const downRight = calculateTrailingZeros(prefixCols[j], postfixRow, num);52
// down-left => prefixCol + prefixRow53
const downLeft = calculateTrailingZeros(prefixCols[j], prefixRow, num);54
// up-right => postfixCols + postfixRow55
const upRight = calculateTrailingZeros(postfixCols[j], postfixRow, num);56
// up-left => postfixCols + prefixRow57
const upLeft = calculateTrailingZeros(postfixCols[j], prefixRow, num);59
maxZeros = Math.max(maxZeros, downRight, downLeft, upRight, upLeft);61
prefixRow["2"] += twoCount;62
prefixCols[j]["2"] += twoCount;64
prefixRow["5"] += fiveCount;65
prefixCols[j]["5"] += fiveCount;71
function calculateTrailingZeros(col, row, currNum) {76
twosCount = row["2"] + col["2"];77
fivesCount = row["5"] + col["5"];79
twosCount += getCount(currNum, 2);80
fivesCount += getCount(currNum, 5);81
return Math.min(twosCount, fivesCount);84
function getCount(num, divisor) {87
while (num % divisor === 0) {