当前位置:网站首页>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
边栏推荐
- Go语言自学系列 | golang嵌套结构体
- Wechat: get the owner of a single tag
- xctf刷题小记
- 共享办公室,提升入驻体验
- L2-024 部落 (25 分)(并查集)
- Redis Desktop Manager for Mac(Redis可视化工具)
- Non duplicate data values of two MySQL query tables
- LaTeX数学公式
- Test your machine learning pipeline
- Strength comparison vulnerability of PHP based on hash algorithm
猜你喜欢

1099 establish binary search tree (30 points)

MySQL查询两张表属性值非重复的数据

2022-04-22 openebs cloud native storage

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

MySQL小练习(仅适合初学者,非初学者勿进)

PCTP考试经验分享

Arbre de dépendance de l'emballage des ressources

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

Notes d'apprentissage oneflow: de functor à opexprinterpreter
![Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]](/img/67/1f9df4236b0aac3480836d45ab8561.png)
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
随机推荐
使用flask和h5搭建网站/应用的简要步骤
Summary of solid problems
是否完全二叉搜索树 (30 分)
Reference passing 1
Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
Wechat: get the owner of a single tag
Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing
LeetCode_DFS_中等_1254. 统计封闭岛屿的数目
1099 establish binary search tree (30 points)
php基于哈希算法出现的强弱比较漏洞
Go language self-study series | golang method
深度学习框架中的自动微分及高阶导数
LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
请问中衍期货安全靠谱吗?
Resource packaging dependency tree
How much inventory recording does the intelligent system of external call system of okcc call center need?
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
L2-022 rearrange linked list (25 points) (map + structure simulation)
错误: 找不到或无法加载主类
Cadence process angle simulation, Monte Carlo simulation, PSRR