当前位置:网站首页>LeetCode396.旋转数组
LeetCode396.旋转数组
2022-04-23 08:15:00 【摸鱼の文酱】
个人简介 |
️个人主页:摸鱼の文酱博客主页*️
博客领域:java编程基础,mysql
写作风格:干货,干货,还是tmd的干货
精选专栏:【Java】【mysql】 【算法刷题笔记】
博主的码云gitee,平常博主写的程序代码都在里面。
支持博主:点赞、收藏、留言
作者水平很有限,如果发现错误,一定要及时告知作者哦!感谢感谢!
旋转数组
1.原题链接
2.题目要求
给定一个长度为 n 的整数数组 nums 。
假设 arrk 是数组 nums 顺时针旋转 k 个位置后的数组,我们定义 nums 的 旋转函数 F 为:
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]
返回 F(0), F(1), …, F(n-1)中的最大值 。
生成的测试用例让答案符合 32 位 整数。
样例输入: nums = [4,3,2,6]
样例输出: 26
解释:
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
所以 F(0), F(1), F(2), F(3) 中的最大值是 F(3) = 26 。
3.基础框架
java版本的基础框架代码如下:
class Solution {
public int maxRotateFunction(int[] nums) {
}
}
4.解题思路
1.根据数组旋转我们可以想到将nums复制拼接
2.然后从第一个到第n-1个开始轮流做开头执行那个公式.得到最大前缀和
5.完整代码
class Solution {
//滑动窗口加前缀和
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.涉及算法&总结
滑动窗口 前缀和
版权声明
本文为[摸鱼の文酱]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_53947552/article/details/124348408
边栏推荐
- 通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解
- Flink SQL实现流批一体
- How to generate assembly file
- DOM学习笔记---遍历页面所有元素节点
- Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
- idea底栏打开services
- 《深度学习》学习笔记(八)
- 二维01背包
- 获取TrustedInstaller权限
- Redis master-slave server problem
猜你喜欢
Asan minimalism
QT compilation qtxlsx Library
Shell脚本进阶
项目上传部分
idea配置连接远程数据库MySQL,或者是Navicat连接远程数据库失败问题(已解决)
WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code
Excle plus watermark
Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior
PgSQL wants to implement all kinds of column sub query operations of MySQL
随机推荐
What is RPC
Online app resource download website source code
记录:js删除数组中某一项或几项的几种方法
项目上传部分
四张图弄懂matplotlib的一些基本用法
There are some problems when using numeric type to query string type fields in MySQL
HAL库的RCC简介
Queue (C language / linked list)
分布式消息中间件框架选型-数字化架构设计(7)
对li类数组对象随机添加特性,并进行排序
队列(c语言/链表)
RCC introduction of Hal Library
RPC procedure
Notes on English class (4)
ATSS(CVPR2020)
okcc呼叫中心外呼系统智能系统需要用多大的盘存录音?
PDF with watermark
pgsql想实现mysql一样样的列子查询操作
pdf加水印
面了一圈,整理了这套面试题。。