当前位置:网站首页>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
边栏推荐
猜你喜欢
mysql查询字符串类型的字段使用数字类型查询时问题
LeetCode简单题之计算字符串的数字和
The whole house intelligence bet by the giant is driving the "self revolution" of Hisense, Huawei and Xiaomi
岛屿的个数
Draw a circle quickly in MATLAB (the one that can be drawn directly given the coordinates and radius of the center of the circle)
[极客大挑战 2019]Havefun1
Thinkphp6 + JWT realizes login verification
在线YAML转XML工具
Comparison of indoor positioning methods of several intelligent robots
社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
随机推荐
Jetson Xavier NX(3)Bazel Mediapipe 安装
[go]常见的并发模型[泛型版]
在线YAML转XML工具
sql 使用过的查询语句
php高精度计算
Fibula dynamic programming
vivo,硬件安全的爱与雷霆
智能名片小程序名片详情页功能实现关键代码
How does feign integrate hystrix
欧圣电气深交所上市:市值52亿 陆为东父女为美国籍
thinkphp6+jwt 实现登录验证
Briefly describe the hierarchical strategy of memory
Implementation of new
[programming practice / embedded competition] learning record of embedded competition (I): establishment of TCP server and web interface
三星,再次“西征”
编译原理题-带答案
多目视觉SLAM
网赚APP资源下载类网站源码
Face to face summary 2
几种智能机器人室内定位方法对比