Skip to content

Commit 6b93039

Browse files
authored
Added task 2245.
1 parent 4e0fd20 commit 6b93039

File tree

3 files changed

+125
-6
lines changed

3 files changed

+125
-6
lines changed
Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,57 @@
11
package g2201_2300.s2245_maximum_trailing_zeros_in_a_cornered_path;
22

3-
// #Medium #Array #Matrix #Prefix_Sum
3+
// #Medium #Array #Matrix #Prefix_Sum #2022_06_11_Time_96_ms_(88.49%)_Space_132_MB_(22.37%)
44

55
public class Solution {
6-
public String decode(String value) {
7-
return value;
6+
public int maxTrailingZeros(int[][] grid) {
7+
int m = grid.length;
8+
int n = grid[0].length;
9+
int max = 0;
10+
int[][] row2 = new int[m + 1][n + 1];
11+
int[][] row5 = new int[m + 1][n + 1];
12+
int[][] col2 = new int[m + 1][n + 1];
13+
int[][] col5 = new int[m + 1][n + 1];
14+
int[][] factor2 = new int[m][n];
15+
int[][] factor5 = new int[m][n];
16+
for (int r = 0; r < m; r++) {
17+
for (int c = 0; c < n; c++) {
18+
int factorTwo = findFactor(grid[r][c], 2);
19+
int factorFive = findFactor(grid[r][c], 5);
20+
row2[r + 1][c + 1] = row2[r + 1][c] + factorTwo;
21+
row5[r + 1][c + 1] = row5[r + 1][c] + factorFive;
22+
col2[r + 1][c + 1] = col2[r][c + 1] + factorTwo;
23+
col5[r + 1][c + 1] = col5[r][c + 1] + factorFive;
24+
factor2[r][c] = factorTwo;
25+
factor5[r][c] = factorFive;
26+
}
27+
}
28+
for (int r = 0; r < m; r++) {
29+
for (int c = 0; c < n; c++) {
30+
int cur2 = factor2[r][c];
31+
int cur5 = factor5[r][c];
32+
int up2 = col2[r + 1][c + 1];
33+
int up5 = col5[r + 1][c + 1];
34+
int down2 = col2[m][c + 1] - col2[r][c + 1];
35+
int down5 = col5[m][c + 1] - col5[r][c + 1];
36+
int left2 = row2[r + 1][c + 1];
37+
int left5 = row5[r + 1][c + 1];
38+
int right2 = row2[r + 1][n] - row2[r + 1][c];
39+
int right5 = row5[r + 1][n] - row5[r + 1][c];
40+
max = Math.max(max, Math.min(left2 + up2 - cur2, left5 + up5 - cur5));
41+
max = Math.max(max, Math.min(right2 + up2 - cur2, right5 + up5 - cur5));
42+
max = Math.max(max, Math.min(left2 + down2 - cur2, left5 + down5 - cur5));
43+
max = Math.max(max, Math.min(right2 + down2 - cur2, right5 + down5 - cur5));
44+
}
45+
}
46+
return max;
47+
}
48+
49+
private int findFactor(int a, int b) {
50+
int factors = 0;
51+
while (a % b == 0) {
52+
a /= b;
53+
factors++;
54+
}
55+
return factors;
856
}
957
}
Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
1-
2245\.
1+
2245\. Maximum Trailing Zeros in a Cornered Path
2+
3+
Medium
4+
5+
You are given a 2D integer array `grid` of size `m x n`, where each cell contains a positive integer.
6+
7+
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.
8+
9+
The **product** of a path is defined as the product of all the values in the path.
10+
11+
Return _the **maximum** number of **trailing zeros** in the product of a cornered path found in_ `grid`.
12+
13+
Note:
14+
15+
* **Horizontal** movement means moving in either the left or right direction.
16+
* **Vertical** movement means moving in either the up or down direction.
17+
18+
**Example 1:**
19+
20+
![](https://assets.leetcode.com/uploads/2022/03/23/ex1new2.jpg)
21+
22+
**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]]
23+
24+
**Output:** 3
25+
26+
**Explanation:** The grid on the left shows a valid cornered path.
27+
28+
It has a product of 15 \* 20 \* 6 \* 1 \* 10 = 18000 which has 3 trailing zeros.
29+
30+
It can be shown that this is the maximum trailing zeros in the product of a cornered path.
31+
32+
33+
The grid in the middle is not a cornered path as it has more than one turn.
34+
35+
The grid on the right is not a cornered path as it requires a return to a previously visited cell.
36+
37+
**Example 2:**
38+
39+
![](https://assets.leetcode.com/uploads/2022/03/25/ex2.jpg)
40+
41+
**Input:** grid = [[4,3,2],[7,6,1],[8,8,8]]
42+
43+
**Output:** 0
44+
45+
**Explanation:** The grid is shown in the figure above.
46+
47+
There are no cornered paths in the grid that result in a product with a trailing zero.
48+
49+
**Constraints:**
50+
51+
* `m == grid.length`
52+
* `n == grid[i].length`
53+
* <code>1 <= m, n <= 10<sup>5</sup></code>
54+
* <code>1 <= m * n <= 10<sup>5</sup></code>
55+
* `1 <= grid[i][j] <= 1000`

src/test/java/g2201_2300/s2245_maximum_trailing_zeros_in_a_cornered_path/SolutionTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@
77

88
class SolutionTest {
99
@Test
10-
void test() {
11-
assertThat(new Solution().decode(""), equalTo(""));
10+
void maxTrailingZeros() {
11+
assertThat(
12+
new Solution()
13+
.maxTrailingZeros(
14+
new int[][] {
15+
{23, 17, 15, 3, 20},
16+
{8, 1, 20, 27, 11},
17+
{9, 4, 6, 2, 21},
18+
{40, 9, 1, 10, 6},
19+
{22, 7, 4, 5, 3}
20+
}),
21+
equalTo(3));
22+
}
23+
24+
@Test
25+
void maxTrailingZeros2() {
26+
assertThat(
27+
new Solution().maxTrailingZeros(new int[][] {{4, 3, 2}, {7, 6, 1}, {8, 8, 8}}),
28+
equalTo(0));
1229
}
1330
}

0 commit comments

Comments
 (0)