当前位置:网站首页>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
边栏推荐
- Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
- 还原二叉树 (25 分)
- Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
- Error: cannot find or load main class
- 爬虫使用xpath解析时返回为空,获取不到相应的元素的原因和解决办法
- Kubernetes如何使用harbor拉去私有镜像
- The K neighbors of each sample are obtained by packet switching
- 关于堆的判断 (25 分) 两种插入方式
- bashdb下载安装
- Go语言自学系列 | golang嵌套结构体
猜你喜欢

1099 establish binary search tree (30 points)

Brief steps to build a website / application using flash and H5

STM32 uses Hal library. The overall structure and function principle are introduced

K210 learning notes (II) serial communication between k210 and stm32

Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download

idea打包 jar文件

Bk3633 specification

Consensus Token:web3. 0 super entrance of ecological flow

On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched

Pctp test experience sharing
随机推荐
LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
Brief steps to build a website / application using flash and H5
Search tree judgment (25 points)
Introduction to matlab
【原创】使用System.Text.Json对Json字符串进行格式化
Experimental report on analysis of overflow vulnerability of assembly language and reverse engineering stack
Technological innovation in government affairs in the construction of Digital Government
Study notes of deep learning (8)
MySQL小练习(仅适合初学者,非初学者勿进)
Complete binary search tree (30 points)
Go language self-study series | golang nested structure
STM32 uses Hal library. The overall structure and function principle are introduced
LaTeX数学公式
Concave hull acquisition method based on convex hull of point cloud
爬虫使用xpath解析时返回为空,获取不到相应的元素的原因和解决办法
玩转二叉树 (25 分)
GUI编程简介 swing
mycat配置
Illegal character in scheme name at index 0: