396. Rotate Function
一开始没察觉到0123 3012 2301 而不是 0123 1230 2301 的原因,所以也没找到规律,一怒之下brute-force..
public int maxRotateFunction(int[] A)
{
if(A.length == 0) return 0;
int res = Integer.MIN_VALUE;
for(int i = 0; i < A.length;i++)
{
int temp = 0;
for(int j = i,total = 0; total < A.length; j++,total++)
{
temp += A[j] * total;
if(j == A.length -1) j = -1;
}
res = Math.max(res,temp);
}
return res;
}
昨晚发现是Easy难度的题,那肯定有特殊的办法,于是单击Discuss.发现新结果可以基于上一个结果。
拿 |4 3 2| 6 来说,第一次变动之后
6 |4 3 2|
4 3 2 比上一个循环各多加了1次(40+31+23 => 41+32+23), 在计算完 4 3 2 6的基础上,加一次数组和。此时我们多加了的一个数是6*4,因为这里6被转到前面,和0乘了,所以要减掉。
总之 newRes = preRes + sumOfArray - A[0]*(A.length)
public int maxRotateFunction(int[] A)
{
if(A.length == 0) return 0;
int res = 0;
int sum = 0;
for(int i = 0; i < A.length; i++)
{
sum+=A[i];
res +=A[i]*i;
}
int max = res;
for(int i = 1; i < A.length; i++)
{
res = res + sum - A[A.length-i]*(A.length);
max = Math.max(res,max);
}
return max;
}
Array操作的平移,也算学到点新东西。
396. Rotate Function的更多相关文章
- LeetCode 396. Rotate Function
Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...
- 396. Rotate Function 移动加权求和,取最大值
[抄题]: Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by ...
- 【LeetCode】396. Rotate Function 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rotate-fu ...
- 【leetcode❤python】 396. Rotate Function
#-*- coding: UTF-8 -*- #超时# lenA=len(A)# maxSum=[]# count=0# while count ...
- 396 Rotate Function 旋转函数
给定一个长度为 n 的整数数组 A .假设 Bk 是数组 A 顺时针旋转 k 个位置后的数组,我们定义 A 的“旋转函数” F 为:F(k) = 0 * Bk[0] + 1 * Bk[1] + ... ...
- [LeetCode] Rotate Function 旋转函数
Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...
- Leetcode: Rotate Function
Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...
- [Swift]LeetCode396. 旋转函数 | Rotate Function
Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
随机推荐
- php精粹-编写高效的php代码 --- php设计模式
1.选择一个最合适的设计模式 没有任何事物是完美的,也没有人说过设计模式一个严格的放之四海而皆准的解决方法.因此你可以改变这些模式,使它们更适合手头的工作.对于某些设计模式而言,他们就是所属程序固有的 ...
- appium的安装过程(图文界面)
资料来源:http://www.cnblogs.com/fnng/p/4560298.html 1.准备安装材料
- 斗地主你什么时候才会托管?(.NET中的托管于非托管)
文章部分引自<.NET4.0面向对象编程漫谈(基础篇)>第1章.NET面向对象编程基础(作者:金旭亮) 无意间看到一位四五岁左右小朋友在玩斗地主,总开始到结束,她一直都在使用“提示”(托管 ...
- typedef与define基本使用
参考: typedef用法 http://www.cnblogs.com/ggjucheng/archive/2011/12/27/2303238.html#define用法:http://blog. ...
- 强大DevExpress,Winform LookUpEdit 实现多列查询 gridview弹出下拉选择 z
关键代码请参考http://www.devexpress.com/Support/Center/p/K18333.aspx 最新DEMO 下载 The current GridLookUpEdit's ...
- shutdown computer in ad and ou
1. powershell Shutdown-computer –computername (gc c:\temp\serverlist.txt) –force –throttlelimit 10 h ...
- 【技术贴】Eclipse 右键打开当前文件所在文件夹
1.使用插件,百度:OpenExplorer_1.5.0.v201108051513.jar 2.默认情况下使用eclipse打开当前文件所在文件夹很麻烦,需要右键点击 Package Explore ...
- 移除Ubuntu“下载额外数据不成功”的提示通知
参考自经过几天的摸索,终于得出安装flashplugin-installer的方法 移除"下载额外数据不成功"的恼人提示通知,方法: $cd /usr/share/package- ...
- Entity Framework关联实体的三种加载方法
推荐文章 EF性能之关联加载 总结很好 一:介绍三种加载方式 Entity Framework作为一个优秀的ORM框架,它使得操作数据库就像操作内存中的数据一样,但是这种抽象是有性能代价的,故鱼和熊掌 ...
- layer.js:2 Uncaught TypeError: Cannot read property 'extend' of undefined
在引用layer.js插件进行前端编程的时候,如果报这个错,解决办法只需: 把layer的引用放在有冲突的js库前面就行了