Skip to content

Commit 3d59588

Browse files
committed
feat: 2245. Maximum Trailing Zeros in a Cornered Path : 前缀和
1 parent 9fb0088 commit 3d59588

File tree

1 file changed

+30
-65
lines changed

1 file changed

+30
-65
lines changed

leetcode/contest-289/6072. Maximum Trailing Zeros in a Cornered Path.js

Lines changed: 30 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,42 @@
22
* @param {number[][]} grid
33
* @return {number}
44
*/
5-
const maxTrailingZeros = function (g) {
6-
const m = g.length
7-
const n = g[0].length
8-
// { c2: 前面的数, c5:, c10: 多少 0 }
9-
const el = (c) => {
10-
const com = Math.min(c.c2, c.c5)
11-
c.c2 -= com
12-
c.c5 -= com
13-
c.c10 += com
5+
const maxTrailingZeros = function (grid) {
6+
const m = grid.length; const n = grid[0].length
7+
let ans = 0; const base = 1e7
8+
const sr = []; const sc = []
9+
const cnt = (a) => {
10+
let c2 = 0; let c5 = 0
11+
while (a % 2 === 0) a /= 2, c2++
12+
while (a % 5 === 0) a /= 5, c5++
13+
return [c2, c5]
1414
}
15-
const mul = (a, b) => {
16-
const c = {
17-
c2: a.c2 + b.c2,
18-
c5: a.c5 + b.c5,
19-
c10: a.c10 + b.c10
20-
}
21-
el(c)
22-
return c
23-
}
24-
const div = (a, b) => {
25-
const c = {
26-
c2: a.c2 - b.c2,
27-
c5: a.c5 - b.c5,
28-
c10: a.c10 - b.c10
29-
}
30-
el(c)
31-
return c
32-
}
33-
const get = (c) => {
34-
cc = { c2: 0, c5: 0, c10: 0 }
35-
while (c % 2 === 0) {
36-
cc.c2++
37-
c /= 2
38-
}
39-
while (c % 5 === 0) {
40-
cc.c5++
41-
c /= 5
42-
}
43-
el(c)
44-
return cc
15+
const get = (i, j, k) => {
16+
return cnt(grid[i][j])[k]
4517
}
46-
const sumR = []; const sumC = []
47-
for (let i = 0; i < m; i++) {
48-
sumR[i] = []
49-
sumR[i][-1] = get(1)
50-
for (let j = 0; j < n; j++) {
51-
sumR[i][j] = mul(sumR[i][j - 1], get(g[i][j]))
52-
// console.log(0, i, j, get(g[i][j]), sumR[i][j-1], sumR[i][j])
18+
for (let j = 1; j <= n; j++) {
19+
sr[j] = [0]
20+
for (let i = 1; i <= m; i++) {
21+
sr[j][i] = sr[j][i - 1] + get(i - 1, j - 1, 0) * base + get(i - 1, j - 1, 1)
5322
}
5423
}
55-
for (let j = 0; j < n; j++) {
56-
sumC[j] = []
57-
sumC[j][-1] = get(1)
58-
for (let i = 0; i < m; i++) {
59-
sumC[j][i] = mul(sumC[j][i - 1], get(g[i][j]))
60-
// console.log('1', j, i, sumR[i][j])
24+
for (let i = 1; i <= m; i++) {
25+
sc[i] = [0]
26+
for (let j = 1; j <= n; j++) {
27+
sc[i][j] = sc[i][j - 1] + get(i - 1, j - 1, 0) * base + get(i - 1, j - 1, 1)
6128
}
6229
}
63-
64-
let ans = 0
65-
for (let i = 0; i < m; i++) {
66-
for (let j = 0; j < n; j++) {
67-
// 列 [0, i],行 [j+1, n-1)
68-
const tmp = mul(div(sumR[i][n - 1], sumR[i][j]), sumC[j][i])
69-
// 列 [0, i],行 [0, j-1]
70-
const tmp1 = mul(sumR[i][j - 1], sumC[j][i])
71-
// 列 [i, m-1],行 [0, j-1]
72-
const tmp2 = mul(sumR[i][j - 1], div(sumC[j][m - 1], sumC[j][i - 1]))
73-
// 列 [i, m-1],行 [j+1, n-1)
74-
const tmp3 = mul(div(sumR[i][n - 1], sumR[i][j]), div(sumC[j][m - 1], sumC[j][i - 1]))
75-
ans = Math.max(ans, tmp.c10, tmp1.c10, tmp2.c10, tmp3.c10)
30+
// 枚举转角点
31+
for (let i = 1; i <= m; i++) {
32+
for (let j = 1; j <= n; j++) {
33+
for (const [rs, re] of [[1, i], [i, m]]) {
34+
// 避免重复计算 [i, j] 这个点
35+
for (const [cs, ce] of [[1, j - 1], [j + 1, n]]) {
36+
const a = (Math.floor(sr[j][re] / base) - Math.floor(sr[j][rs - 1] / base)) + (sc[i][ce] / base - sc[i][cs - 1] / base)
37+
const b = (Math.floor(sr[j][re] % base) - Math.floor(sr[j][rs - 1] % base)) + (sc[i][ce] % base - sc[i][cs - 1] % base)
38+
ans = Math.max(ans, Math.min(a, b))
39+
}
40+
}
7641
}
7742
}
7843
return ans

0 commit comments

Comments
 (0)