当前位置:网站首页>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?
- Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
- JVM - Chapter 2 - class loader subsystem
- Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
- What if the server is poisoned? How does the server prevent virus intrusion?
- Configuration of multi spanning tree MSTP
- PHP function
- shell脚本中的DATE日期计算
- 基于 TiDB 的 Apache APISIX 高可用配置中心的最佳实践
- Deeply learn the skills of parameter adjustment
猜你喜欢
【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
单体架构系统重新架构
Spark 算子之filter使用
Redis master-slave replication process
JVM - Chapter 2 - class loader subsystem
Spark 算子之distinct使用
一刷314-剑指 Offer 09. 用两个栈实现队列(e)
Independent operation smart farm Innovation Forum
Vision of building interstellar computing network
Cookie&Session
随机推荐
Best practices of Apache APIs IX high availability configuration center based on tidb
删除字符串中出现次数最少的字符
Deletes the least frequently occurring character in the string
The length of the last word of the string
Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
PHP function
Upgrade MySQL 5.1 to 5.611
Config learning notes component
Large factory technology implementation | industry solution series tutorials
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
Spark 算子之partitionBy
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
Introduction to dynamic programming of leetcode learning plan day3 (198213740)
【递归之数的拆分】n分k,限定范围的拆分
MySQL集群模式与应用场景
MySQL Cluster Mode and application scenario
Go并发和通道
pgpool-II 4.3 中文手册 - 入门教程
怎么看基金是不是reits,通过银行购买基金安全吗
Cookie&Session