当前位置:网站首页>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
边栏推荐
- Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
- Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
- The principle and common methods of multithreading and the difference between thread and runnable
- New developments: new trends in cooperation between smartmesh and meshbox
- Load Balancer
- 考试考试自用
- Redis master-slave replication process
- 【第5节 if和for】
- How do you think the fund is REITs? Is it safe to buy the fund through the bank
- Best practices of Apache APIs IX high availability configuration center based on tidb
猜你喜欢
随机推荐
Cookie&Session
北京某信护网蓝队面试题目
开源项目推荐:3D点云处理软件ParaView,基于Qt和VTK
Application of Bloom filter in 100 million flow e-commerce system
[split of recursive number] n points K, split of limited range
Named in pytoch_ parameters、named_ children、named_ Modules function
【自娱自乐】构造笔记 week 2
The principle and common methods of multithreading and the difference between thread and runnable
时序模型:长短期记忆网络(LSTM)
IronPDF for .NET 2022.4.5455
vim指定行注释和解注释
gps北斗高精度卫星时间同步系统应用案例
WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令
编译,连接 -- 笔记
Merging of Shanzhai version [i]
VIM specifies the line comment and reconciliation comment
Introduction to dynamic programming of leetcode learning plan day3 (198213740)
Upgrade MySQL 5.1 to 5.69
Recommended search common evaluation indicators
String sorting