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 the maximum number of trailing zeros in the product of a cornered path found in grid
.
Note:
Example 1:
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: 3 Explanation: 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 this is 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.
Example 2:
Input: grid = [[4,3,2],[7,6,1],[8,8,8]] Output: 0 Explanation: 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.
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 105
1 <= m * n <= 105
1 <= grid[i][j] <= 1000
This problem requires finding the maximum number of trailing zeros in the product of a cornered path within a given grid. A cornered path is defined as a path that moves horizontally or vertically until it makes at most one turn, then continues in the perpendicular direction without revisiting cells. The number of trailing zeros in a product is determined by the minimum count of factors 2 and 5.
Approach:
The solution utilizes a prefix sum technique to efficiently count the number of factors 2 and 5 in rows and columns. Here's a breakdown:
Prefix Sum Arrays: Four 2D arrays (r2
, c2
, r5
, c5
) are created to store prefix sums of the counts of factors 2 and 5 in rows and columns respectively. r2[i][j]
stores the total number of factor 2s from the beginning of row i
up to column j
. Similar logic applies to c2
, r5
, and c5
.
Counting Factors: The code iterates through the grid
. For each cell value, it counts the number of times 2 and 5 divide the value (using while loops). These counts are then added to the respective prefix sum arrays.
Enumerating Turning Points: The code then iterates through all possible turning points in the grid. For each turning point (i, j)
, it considers four potential cornered paths:
(i,j)
then up.(i,j)
then down.(i,j)
then up.(i,j)
then down.Calculating Trailing Zeros: For each path, the number of trailing zeros is calculated using the prefix sums. For example, for the "right then up" path, the number of trailing zeros is the minimum of the sum of 2s and 5s from the beginning of the row to (i,j)
and the sum of 2s and 5s from the beginning of the column to (i,j)
.
Maximizing Trailing Zeros: The maximum number of trailing zeros among all paths is tracked and returned as the result.
Time Complexity Analysis:
Therefore, the overall time complexity is O(m*n), dominated by the prefix sum calculation and turning point enumeration.
Space Complexity Analysis:
The space complexity is O(m*n) due to the four prefix sum arrays, each having the same dimensions as the input grid.
Code Examples (Python, Java, C++, Go, TypeScript):
The code examples provided in the previous response demonstrate this approach effectively. Each example shows how to implement the prefix sum arrays, count factors, enumerate turning points, and find the maximum number of trailing zeros. The choice of language is primarily a matter of preference; the core algorithm remains consistent. Note that minor variations in implementation details exist between languages, but the fundamental approach remains the same.