当前位置:网站首页>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
边栏推荐
- Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring
- Don't you know the usage scenario of the responsibility chain model?
- LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
- Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
- Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
- do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
- Parameter stack pressing problem of C language in structure parameter transmission
- Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
- 8.5 循环神经网络简洁实现
- epoll 的 ET,LT工作模式———实例程序
猜你喜欢

Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring

555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc

Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)

外包幹了四年,廢了...
![[detailed explanation of factory mode] factory method mode](/img/56/04fa84d0b5f30e759854a39afacff2.png)
[detailed explanation of factory mode] factory method mode

【Servlet】Servlet 详解(使用+原理)

OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt

thinkphp5+数据大屏展示效果
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

A blog allows you to learn how to write markdown on vscode
随机推荐
【Servlet】Servlet 详解(使用+原理)
[servlet] detailed explanation of servlet (use + principle)
Explanation and example application of the principle of logistic regression in machine learning
51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
[jz46 translate numbers into strings]
The art of automation
【工厂模式详解】工厂方法模式
Resolve the conflict between computed attribute and input blur event
qt之.pro文件详解
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
一款不错的工具:aardio
First acquaintance with STL
MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
MySQL error packet out of order
DVWA之暴力破解(Brute Force)Low-->high
多语言通信基础 06 go实现grpc的四种数据流模式实现
When splicing HQL, the new field does not appear in the construction method
面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
阿里研发三面,面试官一套组合拳让我当场懵逼
Find daffodils - for loop practice