当前位置:网站首页>Leetcode-396 rotation function
Leetcode-396 rotation function
2022-04-23 15:48:00 【Mid year and early year boundary】
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
solution :
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)
版权声明
本文为[Mid year and early year boundary]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231545223097.html
边栏推荐
猜你喜欢
How can poor areas without networks have money to build networks?
Redis主从复制过程
【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
大型互联网为什么禁止ip直连
现在做自媒体能赚钱吗?看完这篇文章你就明白了
Merging of Shanzhai version [i]
Advantages, disadvantages and selection of activation function
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
Config组件学习笔记
[open source tool sharing] MCU debugging assistant (oscillograph / modification / log) - linkscope
随机推荐
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
MySQL optimistic lock to solve concurrency conflict
Independent operation smart farm Innovation Forum
【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
utils.DeprecatedIn35 因升级可能取消,该如何办
移动金融(自用)
单体架构系统重新架构
Spark 算子之交集、并集、差集
VIM specifies the line comment and reconciliation comment
One brush 313 sword finger offer 06 Print linked list from end to end (E)
How do you think the fund is REITs? Is it safe to buy the fund through the bank
Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
Application of Bloom filter in 100 million flow e-commerce system
【递归之数的拆分】n分k,限定范围的拆分
Spark 算子之filter使用
多线程原理和常用方法以及Thread和Runnable的区别
时序模型:长短期记忆网络(LSTM)
Go language, array, pointer, structure
Codejock Suite Pro v20.3.0
What if the package cannot be found