当前位置:网站首页>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
边栏推荐
猜你喜欢
KNN, kmeans and GMM
Best practices of Apache APIs IX high availability configuration center based on tidb
Basic concepts of website construction and management
How did the computer reinstall the system? The display has no signal
IronPDF for . NET 2022.4.5455
Multitimer V2 reconstruction version | an infinitely scalable software timer
JVM-第2章-类加载子系统(Class Loader Subsystem)
Sorting and replying to questions related to transformer
C#,贝尔数(Bell Number)的计算方法与源程序
Temporal model: long-term and short-term memory network (LSTM)
随机推荐
Interview questions of a blue team of Beijing Information Protection Network
C language --- string + memory function
Go语言数组,指针,结构体
php函数
Recommended search common evaluation indicators
CAP定理
使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群
What if the server is poisoned? How does the server prevent virus intrusion?
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
GFS distributed file system (Theory)
Neodynamic Barcode Professional for WPF V11. 0
Basic greedy summary
Node. JS ODBC connection PostgreSQL
负载均衡器
导入地址表分析(根据库文件名求出:导入函数数量、函数序号、函数名称)
时序模型:门控循环单元网络(GRU)
提取不重复的整数
PHP function
vim指定行注释和解注释
Rsync + inotify remote synchronization