当前位置:网站首页>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 scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
- DVWA之暴力破解(Brute Force)Low-->high
- 拼接hql时,新增字段没有出现在构造方法中
- How do I open the win10 startup folder?
- Go basic reflection
- qt之.pro文件详解
- 你還不知道責任鏈模式的使用場景嗎?
- MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
- 51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
- do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
猜你喜欢
ASEMI超快恢复二极管与肖特基二极管可以互换吗
we引用My97DatePicker 实现时间插件使用
LeetCode 练习——396. 旋转函数
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
LeetCode165-比较版本号-双指针-字符串
Comment eolink facilite le télétravail
Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
想要成为架构师?夯实基础最重要
随机推荐
Branch statement of process control
Explanation and example application of the principle of logistic regression in machine learning
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
详解TCP的三次握手
博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
Achievements in science and Technology (21)
do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
ASEMI三相整流桥和单相整流桥的详细对比
[servlet] detailed explanation of servlet (use + principle)
Brute force of DVWA low -- > High
牛客网数据库SQL实战详细剖析(26-30)
【无标题】
自动化的艺术
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
When splicing HQL, the new field does not appear in the construction method
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
QT actual combat: Yunxi calendar
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc