当前位置:网站首页>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
边栏推荐
- API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
- Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
- How to test mobile app?
- utils.DeprecatedIn35 因升级可能取消,该如何办
- MySQL Cluster Mode and application scenario
- s16. One click installation of containerd script based on image warehouse
- Node. JS ODBC connection PostgreSQL
- String sorting
- For examination
- How do you think the fund is REITs? Is it safe to buy the fund through the bank
猜你喜欢
随机推荐
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
Merging of Shanzhai version [i]
考试考试自用
建设星际计算网络的愿景
移动金融(自用)
字符串最后一个单词的长度
KNN, kmeans and GMM
How did the computer reinstall the system? The display has no signal
Multitimer V2 reconstruction version | an infinitely scalable software timer
Why disable foreign key constraints
shell脚本中的DATE日期计算
【递归之数的拆分】n分k,限定范围的拆分
Go语言切片,范围,集合
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
Fastjon2他来了,性能显著提升,还能再战十年
GFS distributed file system (Theory)
Special analysis of China's digital technology in 2022
One brush 313 sword finger offer 06 Print linked list from end to end (E)
幂等性的处理
The El tree implementation only displays a certain level of check boxes and selects radio









