当前位置:网站首页>leetcode 396. Rotation function
leetcode 396. Rotation function
2022-04-23 00:39:00 【Sauce bear】
Topic link
Ideas : iteration
analysis : Manually simulate , In fact, we can find the law
First , use sum Make a note of F(0), use k Record the cumulative sum
Simply simulate , Find the law
nums = [4,3,2,6]
F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6)
F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2)
F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3)
F(1) And F(0) comparison , You can see , One more. 4, One more. 3, One more. 2, Less 3 individual 6
F(2) And F(1) comparison , You can see , One more. 4, One more. 3, One more. 6, Less 3 individual 2
F(1) = F(0)+(k-nums[n-1])-(n-1)*nums[n-1]
F(2) = F(0)+(k-nums[n-2])-(n-1)nums[n-2]
Get the law :F(x)=F(x-1)+k-nnums[n-x]; (1<=x<n)
Code :
class Solution {
public int maxRotateFunction(int[] nums) {
int max = Integer.MIN_VALUE;
int preSum = 0;
int sum=0;
int k=0;
int len = nums.length;
// initialization F(0) Sum cumulative sum
for(int i =0; i<len; i++){
sum = sum + i * nums[i];
k+=nums[i];
}
max = sum;
for(int i=1;i<len;i++){
preSum = sum;
sum = preSum-len*nums[len-i]+k;
max = Math.max(max,sum);
}
return max;
}
}
版权声明
本文为[Sauce bear]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230036003108.html
边栏推荐
- L2-011 playing with binary tree (25 points)
- Xamarin效果第二十二篇之录音效果
- Research progress of Lake remote sensing (Overview)
- 深度学习基础学习-残差
- L2-007 家庭房产 (25 分) 并查集或图的遍历
- C# WPF UI框架MahApps切换主题
- 2022年4月22日,第15天
- Research Progress on remote sensing of vegetation phenological parameters (good article sharing)
- Kotlin - 继承 open
- ArcGIS urban living area land suitability evaluation (III)
猜你喜欢

Research on swarm intelligence collaborative work and cognitive computing technology

【图像分类】 一文读懂AlexNet

Linear basis (various templates + examples)

将 AWS S3 数据迁移至 TiDB Cloud 集群

2.55 - use show on different machines you can access_ Bytes (file show_bytes. C) compile and run the sample code. Determine the byte order used by these machines.

我和TiDB的故事 | 毫无准备地不期而遇,却想说与你相遇好幸运

BUUCTF 穿越时空的思念

MySQL -- operation of database

【征文大赛】TiDB 社区专栏第一届征文大赛,快来一次性集齐所有周边吧!

【图像分类】用最简短的代码复现SeNet,小白一定要收藏(keras,Tensorflow2.x)
随机推荐
C#/. Net uses questpdf operation to generate PDF faster and more efficient!
申请CA证书的步骤
Object size and pointer compression -- JVM
对Indexlookup的理解误区
[AI vision · quick review of today's sound acoustic papers, issue 4] Thu, 21 APR 2022
Calculation of plot ratio by ArcGIS
【图像分类】用最简单的代码复现SENet,初学者一定不要错过(pytorch)
2.57 - Programming show_ short, show_ Long and show_ Double, which print the byte representation of C language objects of types short, long and double respectively. Please try running on several machi
Ten years of working at Google is also a happy decade for me
Introduction and usage of connector and analysis of + + and -- operators
TiDB 在连锁快餐企业丨海量交易与实时分析的应用探索
牛客NC13251模
At the age of 33, he has worked for ten years and was laid off. The so-called experience is not worth money at all
Improvement of ref and struct in C 11
C#/.Net 使用QuestPDF操作生成PDF更快更高效!
L2-002 链表去重 (25 分) 标程
L2-013 red alarm (25 points)
The story of tidb and I met unprepared, but I wanted to say how lucky I was to meet you
24V~48V磁吸轨道灯FS2459替代MP2459方案
C WPF UI framework mahapps switching theme