当前位置:网站首页>Rotation function of leetcode medium problem
Rotation function of leetcode medium problem
2022-04-23 08:14:00 【·Starry Sea】
subject
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
Tips :
n == nums.length
1 <= n <= 10^5
-100 <= nums[i] <= 100
source : Power button (LeetCode)
Their thinking
The practice of the topic is easy to understand , As an example 1 It means that the subscript does not move, and each element of the array moves to the right or the element does not move , Move subscript left , We can complete the requirements of the subject by choosing one fixed and the other .
class Solution:
def maxRotateFunction(self, nums: List[int]) -> int:
index=deque([i for i in range(len(nums))]) # Rotation subscript
MAX=-float('inf')
for i in range(len(nums)):
s=0
for j in range(len(nums)):
s+=nums[j]*index[j]
if s>MAX:
MAX=s
index.append(index.popleft()) # Move subscript left
return MAX

Obviously, all arrays and their subscripts are evaluated each time , Complexity is O(n^2) So it's very easy to time out , Therefore, we need to use mathematical methods to reduce the amount of calculation , If you have no idea, you can write on the paper , When we will F(0),F(1) You'll find it when you write your expression , This is actually before high school n Items and common means , The essence of Gauss's calculation is the reduction of dislocation. .
class Solution:
def maxRotateFunction(self, nums: List[int]) -> int:
s,MAX,S,k=0,-float('inf'),sum(nums),1
for i,j in enumerate(nums):
s+=i*j
for i in range(len(nums)):
if s>MAX:
MAX=s
s+=S-len(nums)*nums[-k]
k+=1
return MAX

版权声明
本文为[·Starry Sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230701590541.html
边栏推荐
- Compiling principle questions - with answers
- AAAI 2022 recruit speakers!!
- Asynchronous learning
- Penetration test interview collection -- HVV---
- PHP high precision computing
- Jetson Xavier NX(3)Bazel Mediapipe 安装
- 有意思的js 代码
- [problem solving] vs2019 solves the problem that the EXE file generated by compilation cannot be opened
- LeetCode中等题之旋转函数
- [appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
猜你喜欢

Cloud computing skills competition -- the first part of openstack private cloud environment

AAAI 2022招募讲者啦!!

Comparison of indoor positioning methods of several intelligent robots

BUFFCTF文件中的秘密1

高精度焊接机械臂定位

干货!以点为形:可微分的泊松求解器

欧圣电气深交所上市:市值52亿 陆为东父女为美国籍

搜一下导航完整程序源码

Thinkphp6 + JWT realizes login verification

分布式服务治理Nacos
随机推荐
多目视觉SLAM
thinkphp6+jwt 实现登录验证
[go] common concurrency model [generic version]
LeetCode简单题之统计字符串中的元音子字符串
Hump naming object
The following program deletes n consecutive words starting from the ith character from the string str
Solidity IDE Remix中文版使用手册
C language learning record -- use and analysis of string function (2)
1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
Data security has become a hidden danger. Let's see how vivo can make "user data" armor again
每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9
mysql查询字符串类型的字段使用数字类型查询时问题
使用 Ingress 实现金丝雀发布
[programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP
[Effective Go 中文翻译] 第一篇
CSV Column Extract列提取
Jetson Xavier NX (3) bazel mediapipe installation
LeetCode 1611. 使整数变为 0 的最少操作次数
Smart business card applet business card details page function implementation key code