当前位置:网站首页>每日一题(2022-04-22)——旋转函数
每日一题(2022-04-22)——旋转函数
2022-04-23 02:06:00 【用脑袋装水】
396. 旋转函数
题目描述:
给定一个长度为 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(k) = 0 * arrk[0] + 1 * arrk[1] + … + (n - 1) * arrk[n - 1]
// k=0 顺时针旋转0个位置 arr0=[4,3,2,6] F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25 // k=1 顺时针旋转1个位置 arr1=[6,4,3,2] F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16 // k=2 顺时针旋转2个位置 arr2=[2,6,4,3] F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23 // k=3 顺时针旋转3个位置 arr3=[3,2,6,4] 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 。
超时解:
思路:
采用双指针,一头一尾,每旋转一次,两个指针左移便是旋转后的切片。
func maxRotateFunction(nums []int) int {
length := len(nums)
if length==1 {
return 0
}
// 每轮两指针左移一位
pointHead := 0
pointTail := length - 1
ans := math.MinInt64
for i := 0; i < length; i++ {
f := 0
head := pointHead
for j := 0; j < length; j++ {
f = j*nums[head] + f
head = (head + 1) % length
}
// 如果头指针后推,为负,就指向队列末尾
pointHead--
if pointHead < 0 {
pointHead = length - 1
}
pointTail--
ans = int(math.Max(float64(ans),float64(f)))
}
return ans
}
正解:
思路:
观察F(0), …, F(k)的规律可得:
每旋转一次数组,都是最后一个值的系数变为了0,而其他值的系数都是+1
所以定义sum用来存放数组和, 而F(k)+sum就等于所有系数都+1, 再减去最后一个变化后的值去掉就就是F(k+1)的值了
以示例1为例:
- sum就是切片内所有元素的和,你每加一次sum,就相当于F(k)的每个系数都+1
例如:
sum = 4 + 3 + 2 + 6 = 15
F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
F(0)+sum = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6)+ 4 + 3 + 2 + 6
= (1 * 4) + (2 * 3) + (3 * 2) + (4 * 6) = 40
而由上面观察到的规律:每旋转一次数组,都是最后一个值的系数变为了0,而其他值的系数都是+1
那么我们观察F(1)相对与F(0)+sum哪里有差别:
F(1) = (1 * 4) + (2 * 3) + (3 * 2)+ (0 * 6)
F(0)+sum = (1 * 4) + (2 * 3) + (3 * 2) + (4 * 6)
可以看到原本最后一个值应该对应的是0*6 而F(0)+sum里却是4*6
所以我们可以得出:
F(1) = F(0) + sum - 最后一个变化后的值 = 25 + 15 - 24 = 16
func maxRotateFunction(nums []int) int {
// sum: 数组内所有元素的和
// curSum: F(k) 就是每次旋转数组后,代入F(k)公式求的那个
sum, curSum, ans := 0, 0, 0
for i := 0; i < len(nums); i++ {
// 求数组和
sum = sum + nums[i]
// 求F(0)
curSum = curSum + i*nums[i]
}
ans = curSum
for i := len(nums) - 1; i > 0; i-- {
// F(K+1) = F(K) + sum - 最后一个变化后的值
curSum = curSum + sum - len(nums)*nums[i]
ans = max(ans, curSum)
}
return ans
}
func max(a, b int) int {
if b > a {
return b
}
return a
}
提交结果:

版权声明
本文为[用脑袋装水]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_52000204/article/details/124352148
边栏推荐
- Use Xdebug breakpoint debugging in postman
- [assembly language] understand "stack" from the lowest point of view
- Realize linear regression with tensorflow (including problems and solutions in the process)
- Dynamic batch processing and static batch processing of unity
- How to "gracefully" measure system performance
- 2022.4.20-----leetcode. three hundred and eighty-eight
- How to change the size of SVG pictures without width in openlayer
- Introduction to micro build low code zero Foundation (lesson 2)
- 从开源爱好者到 Apache 董事,一共分几步?
- 中金财富跟中金公司是一家公司吗,安全吗
猜你喜欢

How to set computer IP?

Realize linear regression with tensorflow (including problems and solutions in the process)

Use Xdebug breakpoint debugging in postman
![App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]](/img/86/77b67fd28d2583e12f397430a9a62a.png)
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]

How to initialize "naming and surname" in C language

Uncover floating-point operations hidden by the ARM compiler

【dpdk】10. Dpdk DNS learning notes

简洁开源的一款导航网站源码

How to change the size of SVG pictures without width in openlayer

Nanny level tutorial on building personal home page (II)
随机推荐
Unicorn bio raised $3.2 million to turn prototype equipment used to grow meat into commercial products
2022.4.10-----leetcode. eight hundred and four
What should I pay attention to when using proxy IP?
Realize linear regression with tensorflow (including problems and solutions in the process)
Is the sinking coffee industry a false prosperity or the eve of a broken situation?
【ValueError: math domain error】
89 logistic回归用户画像用户响应度预测
Leetcode39 combined sum
89 logistic回歸用戶畫像用戶響應度預測
Open3d point cloud processing
从开源爱好者到 Apache 董事,一共分几步?
Is CICC fortune a company with CICC? Is it safe
What are the test steps of dynamic proxy IP?
什么是bgp服务器,有哪些优势?
Thinkphp内核开发盲盒商城源码v2.0 对接易支付/阿里云短信/七牛云存储
openstack 服务的启动
ThinkPHP kernel development blind box mall source code v2 0 docking easy payment / Alibaba cloud SMS / qiniu cloud storage
如何“优雅”的测量系统性能
How to choose a good dial-up server?
在使用代理IP前需要了解哪些分类?