当前位置:网站首页>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
边栏推荐
- Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
- Go language learning notes - language interface | go language from scratch
- Using JS to realize a thousandth bit
- Three ways to create objects in JS
- 653. 两数之和 IV - 输入 BST
- ALV tree (ll LR RL RR) insert delete
- Leetcode0587. Install fence
- GUI, CLI and UNIX Philosophy
- 数据清洗 ETL 工具Kettle的安装
- Flutter's loading animation is more interesting
猜你喜欢

Exclusive thoughts and cases of JS

Kettle experiment (III)

Canary publishing using ingress

Detailed explanation of delete, truncate and drop principles in MySQL database

Go language learning notes - array | go language from scratch
![Cloud computing competition -- basic part of 2020 competition [task 3]](/img/a2/36ff5eafd18534207e6ab01422ea59.png)
Cloud computing competition -- basic part of 2020 competition [task 3]

Summary of wrong questions 1

Vivo, hardware safe love and thunder
![Buuctf [actf2020 freshman competition] include1](/img/47/b8f46037f7e9476b8e01e8d6a7857a.png)
Buuctf [actf2020 freshman competition] include1

Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread
随机推荐
NLLLoss+log_ SoftMax=CE_ Loss
Kettle experiment conversion case
Write down the post order traversal of the ~ binary tree
Set the maximum width of the body, but why does the background color of the body cover the whole page?
How to render web pages
Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread
Image processing in opencv -- Introduction to contour + contour features
Leetcode question bank 78 Subset (recursive C implementation)
Two ways for flutter providers to share data
Kettle实验 (三)
Dropout技术之随机神经元与随机深度
501. 二叉搜索树中的众数
Kettle实验
Sql1 [geek challenge 2019]
nn. Explanation of module class
653. Sum of two IV - input BST
JS prototype chain
SQL used query statements
DMP engine work summary (2021, lightsaber)
Go language learning notes - slice, map | go language from scratch