当前位置:网站首页>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
边栏推荐
- Upgrade MySQL 5.1 to 5.69
- PHP 的运算符
- utils. Deprecated in35 may be cancelled due to upgrade. What should I do
- Load Balancer
- Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
- [backtrader source code analysis 18] Yahoo Py code comments and analysis (boring, interested in the code, you can refer to)
- How did the computer reinstall the system? The display has no signal
- 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
- Pgpool II 4.3 Chinese Manual - introductory tutorial
猜你喜欢
WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令
cadence SPB17.4 - Active Class and Subclass
Modèle de Cluster MySQL et scénario d'application
Pgpool II 4.3 Chinese Manual - introductory tutorial
Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
王启亨谈Web3.0与价值互联网“通证交换”
布隆过滤器在亿级流量电商系统的应用
C#,贝尔数(Bell Number)的计算方法与源程序
Independent operation smart farm Innovation Forum
Configuration of multi spanning tree MSTP
随机推荐
Sorting and replying to questions related to transformer
Go语言切片,范围,集合
MySQL集群模式与应用场景
时序模型:门控循环单元网络(GRU)
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
One brush 312 - simple repetition set - Sword finger offer 03 Duplicate number in array (E)
Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
Connect PHP to MSSQL via PDO ODBC
Named in pytoch_ parameters、named_ children、named_ Modules function
[leetcode daily question] install fence
pgpool-II 4.3 中文手册 - 入门教程
pywintypes.com_error: (-2147221020, ‘无效的语法‘, None, None)
CVPR 2022 quality paper sharing
新动态:SmartMesh和MeshBox的合作新动向
MySQL集群模式與應用場景
fatal error: torch/extension. h: No such file or directory
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
Advantages, disadvantages and selection of activation function
Code live collection ▏ software test report template Fan Wen is here
Treatment of idempotency