当前位置:网站首页>Leetcode exercise - 396 Rotation function
Leetcode exercise - 396 Rotation function
2022-04-23 14:48:00 【SK_ Jaco】
List of articles
1. Title Description
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 * 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 .
Example 1:
Input : nums = [4,3,2,6]
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 .
Example 2:
Input : nums = [100]
Output : 0
2. Ideas
2.1 Code
This question is very interesting , If you use a violent loop, you will be prompted to timeout , Therefore, the formula needs to be analyzed . Analyze with topic examples ,
Further observation and derivation , Adding an element in each step can get the sum of the whole array , namely
F ( 0 ) = 0 ∗ n u m s [ 0 ] + 1 ∗ n u m s [ 1 ] + 2 ∗ n u m s [ 2 ] + 3 ∗ n u m s [ 3 ] F ( 1 ) = 0 ∗ n u m s [ 3 ] + 1 ∗ n u m s [ 0 ] + 2 ∗ n u m s [ 1 ] + 3 ∗ n u m s [ 2 ] = F ( 0 ) + n u m s [ 0 ] + n u m s [ 1 ] + n u m s [ 2 ] − 3 ∗ n u m s [ 3 ] = F ( 0 ) + ( n u m s [ 0 ] + n u m s [ 1 ] + n u m s [ 2 ] + n u m s [ 3 ] ) − 4 ∗ n u m s [ 3 ] F ( 2 ) = 0 ∗ n u m s [ 2 ] + 1 ∗ n u m s [ 3 ] + 2 ∗ n u m s [ 0 ] + 3 ∗ n u m s [ 1 ] = F ( 1 ) + n u m [ 0 ] + n u m [ 1 ] + n u m [ 3 ] − 3 ∗ n u m [ 2 ] = F ( 1 ) + ( n u m [ 0 ] + n u m s [ 2 ] + n u m [ 1 ] + n u m [ 3 ] ) − 4 ∗ n u m [ 2 F ( 3 ) = 0 ∗ n u m s [ 1 ] + 1 ∗ n u m s [ 2 ] + 2 ∗ n u m s [ 3 ] + 3 ∗ n u m s [ 0 ] = F ( 2 ) + n u m [ 1 ] + n u m [ 2 ] + n u m [ 3 ] − 3 ∗ n u m [ 0 = F ( 2 ) + ( n u m s [ 0 ] + n u m [ 1 ] + n u m [ 2 ] + n u m [ 3 ] ) − 4 ∗ n u m [ 0 ] \begin{aligned} F(0)&=0*nums[0]+1*nums[1]+2*nums[2]+3*nums[3]\\ \\ F(1)&=0*nums[3]+1*nums[0]+2*nums[1]+3*nums[2]\\ &=F(0)+nums[0]+nums[1]+nums[2]-3*nums[3]\\ &=F(0)+(nums[0]+nums[1]+nums[2]+nums[3])-4*nums[3]\\ \\ F(2)&=0*nums[2]+1*nums[3]+2*nums[0]+3*nums[1]\\ &=F(1)+num[0]+num[1]+num[3]-3*num[2]\\ &=F(1)+(num[0]+nums[2]+num[1]+num[3])-4*num[2\\ \\ F(3)&=0*nums[1]+1*nums[2]+2*nums[3]+3*nums[0]\\ &=F(2)+num[1]+num[2]+num[3]-3*num[0\\ &=F(2)+(nums[0]+num[1]+num[2]+num[3])-4*num[0]\\ \end{aligned} F(0)F(1)F(2)F(3)=0∗nums[0]+1∗nums[1]+2∗nums[2]+3∗nums[3]=0∗nums[3]+1∗nums[0]+2∗nums[1]+3∗nums[2]=F(0)+nums[0]+nums[1]+nums[2]−3∗nums[3]=F(0)+(nums[0]+nums[1]+nums[2]+nums[3])−4∗nums[3]=0∗nums[2]+1∗nums[3]+2∗nums[0]+3∗nums[1]=F(1)+num[0]+num[1]+num[3]−3∗num[2]=F(1)+(num[0]+nums[2]+num[1]+num[3])−4∗num[2=0∗nums[1]+1∗nums[2]+2∗nums[3]+3∗nums[0]=F(2)+num[1]+num[2]+num[3]−3∗num[0=F(2)+(nums[0]+num[1]+num[2]+num[3])−4∗num[0]
So we get the derivation formula , among i Is the angle mark to be subtracted from the formula
F i ( n ) = F i ( n − 1 ) + ∑ i = 0 n n u m s − n u m s . L e n g t h ∗ n u m s [ i ] F_{i}(n)=F_{i}(n-1)+ \sum_{i=0}^n nums-nums.Length*nums[i] Fi(n)=Fi(n−1)+i=0∑nnums−nums.Length∗nums[i]
The code is as follows :
class Solution {
public int maxRotateFunction(int[] nums) {
int sum = 0;
int max = Integer.MIN_VALUE;
int curr = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
curr += nums[i] * i;
}
max = Math.max(max,curr);
for (int i = nums.length - 1; i > 0; i--) {
curr += sum - nums.length * nums[i];
max = Math.max(max,curr);
}
return max;
}
}
2.2 test result
Pass the test

3. summary
- Violent cycle resolution will timeout
- Observe the example and deduce the formula
版权声明
本文为[SK_ Jaco]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231447507729.html
边栏推荐
- Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
- Thread synchronization, life cycle
- AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc
- Detailed explanation of C language knowledge points -- first knowledge of C language [1]
- Vscode Chinese plug-in doesn't work. Problem solving
- Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
- SVN详细使用教程
- [stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
- A blog allows you to learn how to write markdown on vscode
- Bingbing learning notes: take you step by step to realize the sequence table
猜你喜欢

【NLP】HMM隐马尔可夫+维特比分词

电容

UML project example -- UML diagram description of tiktok

1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)

Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)

Parameter stack pressing problem of C language in structure parameter transmission

Swift protocol Association object resource name management multithreading GCD delay once

编程哲学——自动加载、依赖注入与控制反转

Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge

ASEMI超快恢复二极管与肖特基二极管可以互换吗
随机推荐
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc
51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)
成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
2-Go变量操作
QT Detailed explanation of pro file
LeetCode 练习——396. 旋转函数
We reference My97DatePicker to realize the use of time plug-in
[detailed explanation of factory mode] factory method mode
raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
拼接hql时,新增字段没有出现在构造方法中
Detailed explanation of C language knowledge points -- first knowledge of C language [1]
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
Unity_代码方式添加绑定按钮点击事件
博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)
The initial C language framework is suitable for review and preliminary understanding
SQLSERVER事物与锁的问题