LeetCode : Rotate Function
来源:互联网 发布:php 洗衣管理软件 编辑:程序博客网 时间:2024/04/27 16:19
首先尝试直接计算,记录最大值,会超时。
思路
通过观察
发现function之间有递归关系
Solution
int maxRotateFunction(vector<int>& A){ int k = A.size(); int sum = 0; // A中所有元素和 int res = 0; // 结果:记录最大值 int temp = 0; // function(i) for(int i = 0; i < k; i++) { sum += A[i]; temp += i * A[i]; } res = temp; for(int i = 1; i < k; i++) { temp = temp + sum - k * A[k - i % k]; if(temp > res) res = temp; } return res;}
Tip
前面加一些判断条件,明显减少了最后判定的时间。
if(k == 1) return 0;else if(k == 2) return max(A[0], A[1]);
0 0
- 【Leetcode】396. Rotate Function
- [LeetCode]396. Rotate Function
- <leetcode>396 Rotate Function
- LeetCode 396 Rotate Function
- [Leetcode] Rotate Function
- LeetCode Rotate Function
- LeetCode : Rotate Function
- LeetCode 396. Rotate Function
- [leetcode]396. Rotate Function
- LeetCode-Rotate Function
- [LeetCode]--396. Rotate Function
- leetcode 396. Rotate Function
- [LeetCode] 396. Rotate Function
- LeetCode 396. Rotate Function
- leetcode之Rotate Function
- 45 leetcode - Rotate Function
- leetcode 396. Rotate Function
- 【LeetCode】 396. Rotate Function
- yii 小知识点总结
- 九九乘法口诀表
- 基于迅为4412的一个GPIO输出高低电平的驱动
- 用java实现十六进制代码与RGB互转的颜色转换器
- super在构造函数中的运用
- LeetCode : Rotate Function
- javascript 关于Bom的小实例
- leetcode——Decode String
- java中如何在键盘中输入一串数字然后存入数组中
- 2-7HDFS读取数据过程(Hadoop系列day02)
- Netty将POST、GET参数封装成类使之
- 窗@口@屏@蔽@鼠@标@键@盘@消息
- 文章标题
- 第五周 项目一【建立顺序栈算法库】