一开始没察觉到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的更多相关文章

  1. 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 ...

  2. 396. Rotate Function 移动加权求和,取最大值

    [抄题]: Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by ...

  3. 【LeetCode】396. Rotate Function 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rotate-fu ...

  4. 【leetcode❤python】 396. Rotate Function

    #-*- coding: UTF-8 -*- #超时#        lenA=len(A)#        maxSum=[]#        count=0#        while count ...

  5. 396 Rotate Function 旋转函数

    给定一个长度为 n 的整数数组 A .假设 Bk 是数组 A 顺时针旋转 k 个位置后的数组,我们定义 A 的“旋转函数” F 为:F(k) = 0 * Bk[0] + 1 * Bk[1] + ... ...

  6. [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 ...

  7. 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 ...

  8. [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 ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

随机推荐

  1. 解决inline-block属性带来的标签间间隙问题

    1.给inline-block元素设置一个父元素. 设置父元素的font-size:0:.子元素font-size设置成合适大小,如果不设置子元素font-size,子元素会继承父元素的0: 2.给i ...

  2. [转载]如何重装Grub,使其可以引导双系统

    引言: GRUB是一个多重操作系统的启动管理器.用来引导不同的系统,如windows,Linux.一般来说要先装Windows,后装Linux,这样grub才能生效(grub存在于linux的安装中) ...

  3. kindeditor编辑器代码过滤解决方法.

    很多朋友在使用Kindeditor编辑器的时候都会遇到这样一个问题,如:给A标签加上title属性过后,浏览的时候,却神奇般地发现title属性没有了.再次切换html源代码的时候,返现编辑器将tit ...

  4. Vim粘贴代码时缩进混乱

    Vim粘贴代码时缩进混乱 via 背景 在终端Vim中粘贴代码时,发现插入的代码会有多余的缩进,而且会逐行累加.原因是终端把粘贴的文本存入键盘缓存(Keyboard Buffer)中,Vim则把这些内 ...

  5. 如何利用SecureCRT连接Ubuntu12.0.4

    环境描述:虚拟机网络选择NAT连接方式,Ubuntu的版本是Ubuntu12.0.4 1. 先做一个测试,假设现在系统还没有装ssh,用secureCRT连接Ubuntu是出现下面的界面. 实际上,这 ...

  6. Quartz1.8.5例子(十四)

    org.quartz.scheduler.instanceName: PriorityExampleScheduler # Set thread count to 1 to force Trigger ...

  7. htm、html、shtml区别

    htm.html.shtml都是静态网页的后缀,三者也可以说都是只是扩展名不同,其他一样,都是静态的网页. htm和html是完全静态的网页不通过服务器编译解释直接送出给浏览器读取的静态网页,以htm ...

  8. [原博客] POJ 2975 Nim 统计必胜走法个数

    题目链接题意介绍了一遍Nim取石子游戏,可以看上一篇文章详细介绍.问当前状态的必胜走法个数,也就是走到必败状态的方法数. 我们设sg为所有个数的Xor值.首先如果sg==0,它不可能有必胜走法,输出0 ...

  9. Linux下fork()、vfork()、clone()和exec()的区别

    转自Linux下fork().vfork().clone()和exec()的区别 前三个和最后一个是两个类型.前三个主要是Linux用来创建新的进程(线程)而设计的,exec()系列函数则是用来用指定 ...

  10. Android 小知识点(持续更新)

    ①文件保存默认是private权限. ②在layout的xml文件中onClick的方法中包含一个View类型的参数 ③获取项目下files路径:Context.getFilesDir(); ④获取项 ...