You are given a 2D integer array grid of size m x n, where each cell contains a positive integer.
A cornered path is defined as a set of adjacent cells with at most one turn. More specifically, the path should exclusively move either
horizontally or vertically up to the turn (if there is one), without returning to a previously visited cell. After the turn, the path will then move exclusively in the alternate direction: move vertically if it moved horizontally, and vice versa, also without returning to a previously visited cell.
The product of a path is defined as the product of all the values in the path.
Return _themaximum number of trailing zeros in the product of a cornered path found in _grid.
Note:
Horizontal movement means moving in either the left or right direction.
Vertical movement means moving in either the up or down direction.
Input: grid =[[23,17,15,3,20],[8,1,20,27,11],[9,4,6,2,21],[40,9,1,10,6],[22,7,4,5,3]]Output: 3Explanation: The grid on the left shows a valid cornered path.It has a product of 15*20*6*1*10=18000 which has 3 trailing zeros.It can be shown that thisis the maximum trailing zeros in the product of a cornered path.The grid in the middle is not a cornered path as it has more than one turn.The grid on the right is not a cornered path as it requires a return to a previously visited cell.
Input: grid =[[4,3,2],[7,6,1],[8,8,8]]Output: 0Explanation: The grid is shown in the figure above.There are no cornered paths in the grid that result in a product with a trailing zero.
Trailing zeros in a product are determined by the minimum number of times 2 and 5 appear in its prime factorization. For each path, we need to count the total number of 2s and 5s in the product. By precomputing prefix sums for factors of 2 and 5 in all rows and columns, we can efficiently calculate the number of trailing zeros for any cornered path.
For each cell, count the number of 2s and 5s in its value.
Build prefix sums for factors of 2 and 5 for each row and column.
For each cell, consider all four possible cornered paths (up+right, up+left, down+right, down+left) and calculate the total number of 2s and 5s in the path using prefix sums.
The number of trailing zeros for a path is the minimum of the total number of 2s and 5s.
classSolution {
publicintmaxTrailingZeros(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[][] twos =newint[m][n], fives =newint[m][n];
for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) {
int x = grid[i][j];
while (x % 2 == 0) { twos[i][j]++; x /= 2; }
x = grid[i][j];
while (x % 5 == 0) { fives[i][j]++; x /= 5; }
}
int[][] row2 =newint[m+1][n], row5 =newint[m+1][n], col2 =newint[m][n+1], col5 =newint[m][n+1];
for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) {
row2[i+1][j]= row2[i][j]+ twos[i][j];
row5[i+1][j]= row5[i][j]+ fives[i][j];
col2[i][j+1]= col2[i][j]+ twos[i][j];
col5[i][j+1]= col5[i][j]+ fives[i][j];
}
int ans = 0;
for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) {
int a = row2[i+1][j]+ col2[i][n]- col2[i][j+1];
int b = row5[i+1][j]+ col5[i][n]- col5[i][j+1];
ans = Math.max(ans, Math.min(a, b));
a = row2[m][j]- row2[i][j]+ col2[i][n]- col2[i][j+1];
b = row5[m][j]- row5[i][j]+ col5[i][n]- col5[i][j+1];
ans = Math.max(ans, Math.min(a, b));
a = row2[i+1][j]+ col2[i][j+1];
b = row5[i+1][j]+ col5[i][j+1];
ans = Math.max(ans, Math.min(a, b));
a = row2[m][j]- row2[i][j]+ col2[i][j+1];
b = row5[m][j]- row5[i][j]+ col5[i][j+1];
ans = Math.max(ans, Math.min(a, b));
}
return ans;
}
}
classSolution {
funmaxTrailingZeros(grid: Array<IntArray>): Int {
val m = grid.size
val n = grid[0].size
val twos = Array(m) { IntArray(n) }
val fives = Array(m) { IntArray(n) }
for (i in0 until m) for (j in0 until n) {
var x = grid[i][j]
while (x % 2==0) { twos[i][j]++; x /=2 }
x = grid[i][j]
while (x % 5==0) { fives[i][j]++; x /=5 }
}
val row2 = Array(m+1) { IntArray(n) }
val row5 = Array(m+1) { IntArray(n) }
val col2 = Array(m) { IntArray(n+1) }
val col5 = Array(m) { IntArray(n+1) }
for (i in0 until m) for (j in0 until n) {
row2[i+1][j] = row2[i][j] + twos[i][j]
row5[i+1][j] = row5[i][j] + fives[i][j]
col2[i][j+1] = col2[i][j] + twos[i][j]
col5[i][j+1] = col5[i][j] + fives[i][j]
}
var ans = 0for (i in0 until m) for (j in0 until n) {
var a = row2[i+1][j] + col2[i][n] - col2[i][j+1]
var b = row5[i+1][j] + col5[i][n] - col5[i][j+1]
ans = maxOf(ans, minOf(a, b))
a = row2[m][j] - row2[i][j] + col2[i][n] - col2[i][j+1]
b = row5[m][j] - row5[i][j] + col5[i][n] - col5[i][j+1]
ans = maxOf(ans, minOf(a, b))
a = row2[i+1][j] + col2[i][j+1]
b = row5[i+1][j] + col5[i][j+1]
ans = maxOf(ans, minOf(a, b))
a = row2[m][j] - row2[i][j] + col2[i][j+1]
b = row5[m][j] - row5[i][j] + col5[i][j+1]
ans = maxOf(ans, minOf(a, b))
}
return ans
}
}
defmax_trailing_zeros(grid: list[list[int]]) -> int:
m, n = len(grid), len(grid[0])
twos = [[0]*n for _ in range(m)]
fives = [[0]*n for _ in range(m)]
for i in range(m):
for j in range(n):
x = grid[i][j]
while x %2==0:
twos[i][j] +=1 x //=2 x = grid[i][j]
while x %5==0:
fives[i][j] +=1 x //=5 row2 = [[0]*n for _ in range(m+1)]
row5 = [[0]*n for _ in range(m+1)]
col2 = [[0]*(n+1) for _ in range(m)]
col5 = [[0]*(n+1) for _ in range(m)]
for i in range(m):
for j in range(n):
row2[i+1][j] = row2[i][j] + twos[i][j]
row5[i+1][j] = row5[i][j] + fives[i][j]
col2[i][j+1] = col2[i][j] + twos[i][j]
col5[i][j+1] = col5[i][j] + fives[i][j]
ans =0for i in range(m):
for j in range(n):
a = row2[i+1][j] + col2[i][n] - col2[i][j+1]
b = row5[i+1][j] + col5[i][n] - col5[i][j+1]
ans = max(ans, min(a, b))
a = row2[m][j] - row2[i][j] + col2[i][n] - col2[i][j+1]
b = row5[m][j] - row5[i][j] + col5[i][n] - col5[i][j+1]
ans = max(ans, min(a, b))
a = row2[i+1][j] + col2[i][j+1]
b = row5[i+1][j] + col5[i][j+1]
ans = max(ans, min(a, b))
a = row2[m][j] - row2[i][j] + col2[i][j+1]
b = row5[m][j] - row5[i][j] + col5[i][j+1]
ans = max(ans, min(a, b))
return ans