当前位置:网站首页>Leetcode0396. Rotation function (medium, iteration)
Leetcode0396. Rotation function (medium, iteration)
2022-04-22 08:59:00 【Slow ploughing of stupid cattle】
Catalog
1. Problem description
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
Tips :
n == nums.length1 <= n <= 10^5-100 <= nums[i] <= 100
2. Problem solving analysis
Of course, you can get the answer . however , Should not be satisfied with this .
however , Considering the characteristics of rotation , We can get the following recurrence relation :

More generally,
3. Code implementation
from typing import List
class Solution:
def maxRotateFunction(self, nums: List[int]) -> int:
if len(nums)==1:
return 0
# Calculate F(0) and sum of nums
nums_sum = 0
F = 0
N = len(nums)
for k in range(N):
nums_sum += nums[k]
F += k*nums[k]
maxvalue = F
for k in range(1,N):
print(k,F)
F = nums_sum + F - N * nums[(N-k)%N]
maxvalue = F if F> maxvalue else maxvalue
return maxvalue
if __name__ == "__main__":
sln = Solution()
nums = [4,3,2,6]
print(sln.maxRotateFunction(nums))
Execution time :264 ms, In all Python3 Defeated in submission 89.73% Users of
Memory consumption :22 MB, In all Python3 Defeated in submission 61.98% Users of
Go back to the general catalogue : Slow ploughing by stupid cattle Leetcode General catalogue of daily questions ( Dynamic update ...)
版权声明
本文为[Slow ploughing of stupid cattle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220802102668.html
边栏推荐
- Fabric test example, encountered order exited (x) x seconds
- C 位开始公测啦
- Blue Bridge Cup: dance of sine [Jav language implemented recursively]
- How to download pycharm
- CSDN如何转载文章
- Flume composition, put transaction, take transaction
- MyCms 自媒体 CMS 系统 v3.2.2,广告插件优化
- flume做采集的话,怎么解决小文件过多的问题
- 六款很6的电脑驱动管理器:驱动升级用什么软件好 | 国外最好的电脑驱动管理软件推荐
- Usage of static [detailed explanation]
猜你喜欢
随机推荐
Install_ FAILED_ MISSING_ SHARED_ LIBRARY
虾皮二面:什么是零拷贝?如何实现零拷贝?
What are the common collections?
Flume composition, put transaction, take transaction
Wechat applet: typeerror: cannot read property 'Mark' of undefined
花书《深度学习》与代码实现:01 线性代数:基本概念+代码实现基本运算
flume做采集的话,怎么解决小文件过多的问题
flink 学习(十一)Watermark
Rhel7 logical volume management - Notes
解读智慧农业未来发展
工业缺陷检测项目实战(二)——基于深度学习框架yolov5的钢铁表面缺陷检测
那些年不会做的数学题,用Pyhon只需要1分钟?
CSDN如何转载文章
选择排序及优化
素数求解的N种境界
Quick sequencing and optimization
快速排序及优化
Const modifier variable
Shrimp noodles: what is zero copy? How to achieve zero copy?
The server is set to start automatically and regularly







![[path of system analyst] real topic of case analysis of system analyst in 2020](/img/cd/72aefb58df3eebfb9b3a13c53d10b7.jpg)

