当前位置:网站首页>leetcode-396 旋转函数
leetcode-396 旋转函数
2022-04-23 15:45: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
解法:
class Solution:
def maxRotateFunction(self, nums: List[int]) -> int:
n = len(nums)
total = sum(nums)
dp = [0]*n
dp[0] = sum(num*idx for idx, num in enumerate(nums))
for i in range(1,n):
dp[i] = dp[i-1] + total - nums[-i]*n
return max(dp)
版权声明
本文为[年中初界]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44536215/article/details/124352818
边栏推荐
- APISIX jwt-auth 插件存在错误响应中泄露信息的风险公告(CVE-2022-29266)
- Rsync + inotify remote synchronization
- C#,贝尔数(Bell Number)的计算方法与源程序
- Pytorch中named_parameters、named_children、named_modules函数
- WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令
- Interview questions of a blue team of Beijing Information Protection Network
- Pgpool II 4.3 Chinese Manual - introductory tutorial
- MySQL optimistic lock to solve concurrency conflict
- Sorting and replying to questions related to transformer
- 控制结构(二)
猜你喜欢
随机推荐
Multi level cache usage
Special analysis of China's digital technology in 2022
cadence SPB17. 4 - Active Class and Subclass
APISIX jwt-auth 插件存在错误响应中泄露信息的风险公告(CVE-2022-29266)
utils.DeprecatedIn35 因升级可能取消,该如何办
建设星际计算网络的愿景
[split of recursive number] n points K, split of limited range
通過 PDO ODBC 將 PHP 連接到 MySQL
控制结构(一)
Upgrade MySQL 5.1 to 5.69
布隆过滤器在亿级流量电商系统的应用
vim指定行注释和解注释
What are the mobile app software testing tools? Sharing of third-party software evaluation
shell_2
多级缓存使用
JVM-第2章-类加载子系统(Class Loader Subsystem)
One brush 312 - simple repetition set - Sword finger offer 03 Duplicate number in array (E)
CAP定理
Modèle de Cluster MySQL et scénario d'application
大厂技术实现 | 行业解决方案系列教程