当前位置:网站首页>Golang force buckle leetcode 396 Rotation function
Golang force buckle leetcode 396 Rotation function
2022-04-23 09:32:00 【cheems~】
396. Rotation function
396. Rotation function
Answer key
subject : Give an array , Calculation f,f= Subscript * value Accumulation , Each time the number of the array is moved to the end of the front , For the biggest f
Ideas :
f(0)=0*nums[0]+1*nums[1]+2*nums[2]+...+(n-1)*nums[n-1]
f(1)=0*nums[n-1]+1*nums[0]+2*nums[1]+...+(n-1)*nums[n-2]
f(0)=0*nums[0]+1*nums[1]+2*nums[2]+...+(n-1)*nums[n-1]
f(1)=1*nums[0]+2*nums[1]+3*nums[2]+...+0*nums[n-1]
f(1)-f(0)=nums[0]+nums[1]+nums[2]-(n-1)*nums[n-1]
=nums[0]+nums[1]+nums[2]+nums[n-1]-n*nums[n-1]
set up numSum=nums[0]+...+nums[n-1]
have to f(1)-f(0)=numSum-n*nums[n-1]
Get the general formula f(i)-f(i-1)=numSum-n*nums[n-i]
f(i)=f(i-1)+numSum-n*nums[n-k]
Code
func maxRotateFunction(nums []int) int {
numSum, f, n := 0, 0, len(nums)
for i, v := range nums {
numSum += v
f += i * v
}
//numSum=nums[0]+...+nums[n-1]
//f(i)=f(i-1)+numSum-n*nums[n-i]
ans := f
for i := 1; i < len(nums); i++ {
f = f + numSum - n*nums[n-i]
ans = max(ans, f)
}
return ans
}
func max(i, j int) int {
if i > j {
return i
}
return j
}
版权声明
本文为[cheems~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230928449370.html
边栏推荐
- JS what is an event? Event three elements and operation elements
- 112. 路径总和
- MySQL of database -- overview and installation
- Kettle experiment
- [reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)
- Emuelec compilation summary
- npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’
- Kettle experiment (III)
- ALV tree (ll LR RL RR) insert delete
- AQS & reentrantlock implementation principle
猜你喜欢
随机推荐
kettle实验
ABAP CDs view with association example
《信息系统项目管理师总结》第八章 项目干系人管理
AQS & reentrantlock implementation principle
Secrets in buffctf file 1
Kettle experiment (III)
Buuctf [actf2020 freshman competition] include1
Node installation
Detailed explanation of delete, truncate and drop principles in MySQL database
Sql1 [geek challenge 2019]
DMP engine work summary (2021, lightsaber)
Kettle实验 (三)
112. Path sum
Leetcode0587. 安装栅栏(difficult)
Three challenges that a successful Devops leader should be aware of
Personal homepage software fenrus
NLLLoss+log_ SoftMax=CE_ Loss
Flutter 的加载动画这么玩更有趣
高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?
JS scope, scope chain, global variables and local variables









![[geek challenge 2019] havefun1](/img/8b/b15bf31771d54db25f24d630e64093.png)