当前位置:网站首页>golang力扣leetcode 2245.转角路径的乘积中最多能有几个尾随零
golang力扣leetcode 2245.转角路径的乘积中最多能有几个尾随零
2022-04-21 23:07:00 【cheems~】
2245.转角路径的乘积中最多能有几个尾随零
2245.转角路径的乘积中最多能有几个尾随零
题解
题目:只能转一次,求路径中每个元素相乘的结果有几个零
思路:
- 正数的乘积结果中尾 0 的个数由乘数中 因子 2,5 的个数中较小的决定,即 尾随零=min(num2,num5)
- 路径要么是横,竖,要么是UL,UR,DL,DR
- 用前缀和维护每一行和每一列因子 22 与因子 55 的数量
- 枚举拐点(i,j)计算答案
代码
func maxTrailingZeros(grid [][]int) int {
//初始化
n := len(grid)
m := len(grid[0])
factor := make([][]pair, n+1) //2和5的数量
x := make([][]pair, n+1) //列 x
y := make([][]pair, n+1) //行 y
for i := 0; i <= len(grid); i++ {
factor[i] = make([]pair, m+1)
x[i] = make([]pair, m+1)
y[i] = make([]pair, m+1)
}
//计算前缀和
for i := 1; i <= n; i++ {
for j := 1; j <= m; j++ {
factor[i][j] = getRes(grid[i-1][j-1])
x[i][j].two = x[i-1][j].two + factor[i][j].two
x[i][j].five = x[i-1][j].five + factor[i][j].five
y[i][j].two = y[i][j-1].two + factor[i][j].two
y[i][j].five = y[i][j-1].five + factor[i][j].five
}
}
//计算答案
ans := 0
for i := 1; i <= n; i++ {
for j := 1; j <= m; j++ {
upAndLeft := min(x[i][j].two+y[i][j].two-factor[i][j].two, x[i][j].five+y[i][j].five-factor[i][j].five)
upAndRight := min(x[i][j].two+y[i][m].two-y[i][j].two, x[i][j].five+y[i][m].five-y[i][j].five)
downAndLeft := min(x[n][j].two+y[i][j].two-x[i][j].two, x[n][j].five+y[i][j].five-x[i][j].five)
downAndRight := min(x[n][j].two+y[i][m].two-x[i-1][j].two-y[i][j].two, x[n][j].five+y[i][m].five-x[i-1][j].five-y[i][j].five)
ans = max(ans, upAndLeft)
ans = max(ans, upAndRight)
ans = max(ans, downAndLeft)
ans = max(ans, downAndRight)
}
}
return ans
}
func getRes(val int) pair {
two, five := 0, 0
a, b := val, val
for a%2 == 0 && a != 0 {
a /= 2
two++
}
for b%5 == 0 && b != 0 {
b /= 5
five++
}
return pair{
two: two, five: five}
}
func min(i, j int) int {
if i > j {
return j
}
return i
}
func max(i, j int) int {
if i > j {
return i
}
return j
}
版权声明
本文为[cheems~]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42956653/article/details/124328545
边栏推荐
- 【中南林业科技大学】【陈】第七周创新题迷宫
- 图像采集卡的概念及作用原理
- How to set blind area in VOS
- 1956年高考数学
- Yarn online dynamic resource tuning
- REM practical development adaptation scheme for mobile web development
- Precautions for self built VNC software
- Teach you to easily solve CSRF Cross Site Request Forgery Attack
- PHP array function extract usage details
- [summary of some tips and shortcut keys in MATLAB]
猜你喜欢

Kubernetes---ConfigMap配置管理

7.2.3 multiple representations of posture, quaternion, Euler angle, rotation matrix and axis angle ros2 in hands on Robotics

2022 electrician (elementary) examination simulated 100 questions and simulated examination

Nacos Registry - service registration and tiered storage

8.4 control of wheel motion of mobile robot in rodf robot modeling

Teach you to easily solve CSRF Cross Site Request Forgery Attack
![OS Experiment 3 [process communication]](/img/78/e89e0819ae345cb464613935508eac.png)
OS Experiment 3 [process communication]

8.3 create a mobile robot by hand in rodf robot modeling

Pytoch framework | torch nn. modules. Module(nn.Module)

Eventbridge integrated cloud service practice
随机推荐
OS实验三【进程通信】
当贝X3色彩对比度好不好,新3.1版本色彩接近原图
MySQL 第3章 SQL基础语法
Detailed explanation of MySQL concurrency parameter adjustment
1141: C language training - a hundred dollars and a hundred chickens_ Pruning cycle
Teach you to easily solve CSRF Cross Site Request Forgery Attack
1956 college entrance examination mathematics
[MQ] starting from scratch to realize mq-01-start of producers and consumers
Apache Flink series - ④ stateful functions
Go111module = on go mod init error (go: cannot determine module path for source directory)
TensorFlow 2.8 安装
Mysql的并发参数调整详解
Basic concepts of audio and video and a simple introduction to ffmpeg
1. MySQL workbench 8.0 installation
Redis advanced: data deletion and elimination strategy, master-slave replication, sentinel mode, cluster, enterprise solution
Why: uncaught referenceerror: effect is not defined
将模型训练外包真的安全吗?新研究:外包商可能植入后门,控制银行放款
Ros2 robot modeling URDF 8.1 URDF unified robot modeling language
APM industry awareness series - 12 - 13
April 22, 2022 Daily: a new face attribute editing framework based on transformer