当前位置:网站首页>【无标题】LeetCode 396 旋转函数[数学 找规律] HERODING的LeetCode之路
【无标题】LeetCode 396 旋转函数[数学 找规律] HERODING的LeetCode之路
2022-04-22 19:57:00 【HERODING23】

解题思路:
一道非常简单的数学逻辑推理题,简单来说就是找规律,每两个连续的F相减,总会有F(i)-F(i-1)=sum+n*nums[n-i],所以依据这条规律,只要把F(0)计算出来,后面的就都直接出来了,根本无需暴力遍历,代码如下:
class Solution {
public:
int maxRotateFunction(vector<int>& nums) {
int n = nums.size();
int sum = 0, F = 0;
for(int i = 0; i < n; i ++) {
sum += nums[i];
F += i * nums[i];
}
int maxSum = F;
for(int i = 1; i < n; i ++) {
F = F + sum - n * nums[n - i];
maxSum = max(maxSum, F);
}
return maxSum;
}
};
版权声明
本文为[HERODING23]所创,转载请带上原文链接,感谢
https://blog.csdn.net/HERODING23/article/details/124337517
边栏推荐
- CVPR 2022: is smile recognition also sexist? Zhejiang University and Wuhan University jointly set up a fairness improvement framework
- Influxdb deletes a piece of data according to time
- Two timing disable processing mechanisms for SMS verification and their differences
- What if I don't understand? Google's 540 billion parameter new model can explain the laugh point and guess the movie through Emoji expression
- CVPR 2022:微笑识别也带性别歧视?浙大武大联合蚂蚁Adobe搞了个公平性提升框架
- Talking about time series database market
- When MySQL designs a table, two timestamp fields are required
- 短信驗證兩種定時禁用處理機制及區別
- 【2022应届生看过来】一个无经验的大学毕业生,可以转行做软件测试吗?
- Dotnet obtains the equipment manufacturer through WMI
猜你喜欢

2022年Postman最被低估的功能,自动化接口测试效率简直无敌

Which brand of sports headphones is better and recommended for easy-to-use sports headphones

Don't say, "I don't like development, so I choose testing."

顶测科技整理转行过程中一定要避免的问题

树的那些破事~模板小结

Why do I suggest you work in a technical position instead of a clerical position, sales

Quarkus dependency injection 9: bean read / write lock

行业趋势远比努力更重要--顶测科技总结

一百多天,0基础自学转行软件测试,从月薪3000到15k,我整理的超全学习指南

哪个品牌的运动耳机比较好、好用的运动耳机推荐
随机推荐
firewalld dbus接口使用指南
2-way can / can FD data recording diagnostic instrument solves the difficulty of accidental errors for enterprises
DNS资源记录详解&权威|递归解析区别(超细致)
Quarkus dependency injection 9: bean read / write lock
[eight part essay] redis cache
Golang本地缓存选型对比及原理总结
《PyTorch深度学习实践》08 加载数据集
Anaconda新建环境并安装GPU版本的pytorch
一文读懂Hoo Smart Chain的可视化公链
How can I apply for convertible bonds? Is it safe to purchase convertible bonds?
Network tunneling technology
开源免费,最好用的3大系统9大防火墙软件安利给你们
海康内置4G卡的摄像头设备无法注册EasyCVR平台是什么原因?
[practice summary]
Why puddingswap may be a strong dark horse in the field of gamefi?
CVPR 2022: is smile recognition also sexist? Zhejiang University and Wuhan University jointly set up a fairness improvement framework
Deux mécanismes de désactivation de la vérification des messages courts et leurs différences
顶测科技整理转行过程中一定要避免的问题
为什么说PuddingSwap或许为成为GameFi领域的一匹强势黑马?
Is it better to do operation or software testing?