396. Rotate Function
来源:互联网 发布:mac桌面出现ds store 编辑:程序博客网 时间:2024/05/16 11:03
题目大意:给定数组A,长度为ASize,分别用【0~ASize-1】作为各项的系数,求最大值。例子比较明了
A = [4, 3, 2, 6]F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.大致思路:
【暴力】分别结算每个位置的F[i],分别比较,然后求最大值,大数据测试集合TLE。
【改进】找规律,发现F[i]-F[i+1]=Sum[0~Asize-1]-n*A[i],发现这个规律之后没有及时的运用,有点遗憾。
进一步找到F[i]-F[j]=[j-i]*Sum[0~Asize-1]-n*Sum[i~j-1];
我运用这个公式,避免了每次都计算F[i],在测试过程中,发现判断条件那里容易溢出,改为除法比较之后才通过,代码如下:
CODE:
后面运用第一个公式,重做了一遍,代码简洁了很多。代码如下:
0 0
- 396. Rotate Function
- 【Leetcode】396. Rotate Function
- [LeetCode]396. Rotate Function
- 396. Rotate Function
- 396. Rotate Function
- LeetCode 396. Rotate Function
- 396. Rotate Function
- 396. Rotate Function
- 396. Rotate Function
- [leetcode]396. Rotate Function
- 396. Rotate Function
- [LeetCode]--396. Rotate Function
- 396. Rotate Function
- leetcode 396. Rotate Function
- [LeetCode] 396. Rotate Function
- 396. Rotate Function
- 396. Rotate Function (python)
- 396. Rotate Function
- Spring Java ajaxfileupload.js 上传头像 3.0
- dwr WebContextFactory 工厂模式
- Hadoop2.7.3完全分布式集群搭建和测试
- 阿里巴巴73款开源产品全向图
- Class 与 Style 绑定内联样式(vue.js)
- 396. Rotate Function
- js/jquery常用方法(replaceALl、new Map()、点击th(表头)或拖动tr只前台排序)
- day35java基础
- 编写简单Hibernate步骤
- 终于!Linaro 加盟 Zephyr 项目
- 自定义动画(仿Win10加载动画)——优化
- SOAP Webservice和RESTful Webservice
- Android 中性能优化工具之TraceView使用总结
- 如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等