一开始没察觉到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. php 接口 implements 使用

    主要对类名,类所拥有的方法,以及所传参数起约束和规范做用,感觉跟php abstract 抽象类又有点像. 一,接口的定义和调用 <?php interface face1 { const pa ...

  2. php url字符转义操作

    遇到一段代码,从数据库里读出来带 \ 字符 需要转义成中文~ 用到url_decode(); //$info 为刚从数据库中读取的二维数组 foreach($info as $key1 => & ...

  3. odoo 的 拉式 和 推式 库链

    推式链的数据定义在  stock.location.path 表,视图定义在 “路线” 界面的 “push rules” 具体可参考  入库设置为  Receipt in 2 steps . push ...

  4. xcopy总是询问是文件名还是目录名

    我需要运行类似xcopy /y a.xml .\pics\b.xml很多次,但xcopy总是问我“文件名还是目录名” 可以这样通过管道来做echo f | xcopy /y a.xml .\pics\ ...

  5. QQ宠物吹泡泡游戏小助手 VC++6.0代码分析

    最近玩QQ宠物,他总是心情低落,让我很不爽,让他玩耍吧,还得自己点鼠标,所以想偷个懒,试试能不能编个程序让电脑帮我做这个事情. 要干这件事就得先找一个游戏开刀,刚开始我找的是弹力球游戏,不就是点鼠标么 ...

  6. RegexKitLite 使用详解

    1.去RegexKitLite下载类库,解压出来会有一个例子包及2个文件,其实用到的就这2个文件,添加到工程中. 2.工程中添加libicucore.dylib frameworks. 友情提醒:一般 ...

  7. Python的数字类型及其技巧

    Python中的数字类型 int float fractions.Fraction decimal.Decimal 数字的舍与入 int(f):舍去小数部分,只保留整数部分,所以int(-3.8)的结 ...

  8. spirng MVC乱码过滤器

    <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>or ...

  9. mysql connect

    def connect(_host, _user, _passwd, _db, _charset, _port): conn = MySQLdb.connect(host=_host, user=_u ...

  10. DLL导出与调用约定

    一般来说,从DLL导出函数有两种方法.一种是使用.def文件:另一种是使用__declspec(dllexport). 使用上面两种方法各有优缺点.使用.def文件就是需要额外维护,当导出函数更改名字 ...