当前位置:网站首页>LeetCode中等题之旋转函数
LeetCode中等题之旋转函数
2022-04-23 07:02:00 【·星辰大海】
题目
给定一个长度为 n 的整数数组 nums 。
假设 arrk 是数组 nums 顺时针旋转 k 个位置后的数组,我们定义 nums 的 旋转函数 F 为:
F(k) = 0 * arrk[0] + 1 * arrk[1] + … + (n - 1) * arrk[n - 1]
返回 F(0), F(1), …, F(n-1)中的最大值 。
生成的测试用例让答案符合 32 位 整数。
示例 1:
输入: nums = [4,3,2,6]
输出: 26
解释:
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
所以 F(0), F(1), F(2), F(3) 中的最大值是 F(3) = 26 。
示例 2:
输入: nums = [100]
输出: 0
提示:
n == nums.length
1 <= n <= 10^5
-100 <= nums[i] <= 100
来源:力扣(LeetCode)
解题思路
题目的做法比较易懂,按照示例1的意思就是下标不动数组每个元素右移或者元素不动,下标左移,我们任取一种固定另外一种就可以完成题目的要求。
class Solution:
def maxRotateFunction(self, nums: List[int]) -> int:
index=deque([i for i in range(len(nums))]) #旋转下标
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()) #下标左移
return MAX

很明显每次都计算全部的数组和其下标,复杂度是O(n^2)所以非常容易超时,因此我们需要利用数学上的方法使得计算量减少,没有思路的话可以在纸上写写划划,当我们将F(0),F(1)的表达式写出来的时候就会发现,这其实就是高中前n项和常用的手段,高斯当年计算的精华错位相减。
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

版权声明
本文为[·星辰大海]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_18560985/article/details/124352987
边栏推荐
- Flatten arrays
- Canvas learning Chapter 1
- Cloud computing skills competition -- the first part of openstack private cloud environment
- C outputs a two-dimensional array with the following characteristics.
- Intranet penetration series: icmptunnel of Intranet tunnel (Master James Barlow's)
- Move layout (Flex layout, viewport label)
- 搜一下导航完整程序源码
- Positioning of high precision welding manipulator
- 利用Js实现一个千分位
- Depth of binary tree
猜你喜欢

BUFFCTF文件中的秘密1

Comparison of indoor positioning methods of several intelligent robots

Positioning of high precision welding manipulator

1216_ MISRA_ C standard learning notes_ Rule requirements for control flow

idea:使用easyYapi插件导出yapi接口

一款拥有漂亮外表的Typecho简洁主题_Scarfskin 源码下载

Research on software security based on NLP (2)

在MATLAB中快速画圆(给出圆心坐标和半径就能直接画的那种)

高精度焊接机械臂定位
![[appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test](/img/4a/c741ec4f9aa724e150a5ae24d0f9e9.png)
[appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
随机推荐
校园转转二手市场源码下载
PHP generates short links: convert numbers to letters and letters to numbers
Upload labs range practice
多目视觉SLAM
在MATLAB中快速画圆(给出圆心坐标和半径就能直接画的那种)
利用Js实现一个千分位
欧圣电气深交所上市:市值52亿 陆为东父女为美国籍
在线YAML转XML工具
nn.Module类的讲解
Ignis公链的NFT生态发展:Unicorn.art的捐赠开发之路
一篇文章看懂变量提升(hoisting)
Go语学习笔记 - 结构体 | 从零开始Go语言
简述存储器的分级策略
CSV Column Extract列提取
Comparison of indoor positioning methods of several intelligent robots
Flutter之Provider共享数据的两种方式
Smart business card applet business card details page function implementation key code
有意思的js 代码
1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
Talking about distributed storage from ES, mongodb, redis and rocketmq