当前位置:网站首页>LeetCode396. Rotate array
LeetCode396. Rotate array
2022-04-23 08:54:00 【Fish culture sauce】
| Personal profile |
️ Personal home page : loaf on a job の Wenjiang blog home page *️
The blogosphere :java Programming based ,mysql
Writing style : dried food , dried food , still tmd Dry goods of
Select columns :【Java】【mysql】 【 Algorithm brush problem note 】
Blogger's code cloud gitee, Usually the program code written by bloggers is inside .
Support bloggers : give the thumbs-up 、 Collection 、 Leaving a message.
The author's level is very limited , If an error is found , Be sure to inform the author in time ! Thank you thank you !
List of articles
Rotated array
1. Original link
2. Subject requirements
Given a length of n Array of integers for nums .
hypothesis arrk It's an array nums Clockwise rotation k Array after position , We define nums Of Rotation function F by :
F ( k ) = 0 ∗ a r r k [ 0 ] + 1 ∗ a r r k [ 1 ] + . . . + ( n − 1 ) ∗ a r r k [ n − 1 ] F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1] F(k)=0∗arrk[0]+1∗arrk[1]+...+(n−1)∗arrk[n−1]
return F(0), F(1), …, F(n-1) Maximum of .
The generated test cases make the answers meet the requirements 32 position Integers .
The sample input : nums = [4,3,2,6]
Sample output : 26
explain :
F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26
therefore F(0), F(1), F(2), F(3) The maximum value in is F(3) = 26 .
3. Basic framework
java The basic framework code of version is as follows :
class Solution {
public int maxRotateFunction(int[] nums) {
}
}
4. Their thinking
1. According to the rotation of the array, we can think of nums Copy splice
2. Then from the first to the n-1 Take turns at the beginning to execute the formula . Get the maximum prefix and
5. Complete code
class Solution {
// Sliding window prefixes and
public int maxRotateFunction(int[] nums) {
int n = nums.length;
int[] sum = new int[n * 2 + 10];
for (int i = 1; i <= 2 * n; i++) sum[i] = sum[i - 1] + nums[(i - 1) % n];
int ans = 0;
for (int i = 1; i <= n; i++) ans += nums[i - 1] * (i - 1);
for (int i = n + 1, cur = ans; i < 2 * n; i++) {
cur += nums[(i - 1) % n] * (n - 1);
cur -= sum[i - 1] - sum[i - n];
if (cur > ans) ans = cur;
}
return ans;
}
}
6. It's about algorithms & summary
The sliding window The prefix and
版权声明
本文为[Fish culture sauce]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230815168616.html
边栏推荐
- Resource packaging dependency tree
- valgrind和kcachegrind使用运行分析
- 是否完全二叉搜索树 (30 分)
- 单片机数码管秒表
- Wechat: get the owner of a single tag
- Go language self-study series | golang structure pointer
- The crawler returns null when parsing with XPath. The reason why the crawler cannot get the corresponding element and the solution
- On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
- 共享办公室,提升入驻体验
- Download and install bashdb
猜你喜欢

Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library

Correct method of calculating inference time of neural network

【58】最后一个单词的长度【LeetCode】

计算神经网络推理时间的正确方法

LLVM之父Chris Lattner:编译器的黄金时代

資源打包關系依賴樹

L2-3 romantic silhouette (25 points)

After a circle, I sorted out this set of interview questions..

Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting

Redis Desktop Manager for Mac(Redis可视化工具)
随机推荐
扣缴义务人
【原创】使用System.Text.Json对Json字符串进行格式化
政务中台研究目的建设目标,建设意义,技术创新点,技术效果
dataBinding中使用include
【58】最后一个单词的长度【LeetCode】
关于cin,scanf和getline,getchar,cin.getline的混合使用
Judgment on heap (25 points) two insertion methods
Notes on xctf questions
Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting
Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board
Go language self-study series | golang method
L2-022 重排链表 (25 分)(map+结构体模拟)
根据后序和中序遍历输出先序遍历 (25 分)
Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
xctf刷题小记
Experimental report on analysis of overflow vulnerability of assembly language and reverse engineering stack
PLC的点表(寄存器地址和点表定义)破解探测方案--方便工业互联网数据采集
Test your machine learning pipeline
Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing
单片机数码管秒表