一开始没察觉到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. 简易的RestClient代码

    package tests; import java.io.*; import org.apache.http.HttpEntity; import org.apache.http.HttpRespo ...

  2. BJDP结对编程活动

    7月21日参与了 BJDP北京的活动 在北京首次参与能够参与动手编程活动,感觉挺不错的. 本次活动共有三项内容 1.      金锐分享单元测试的Mocking技术,20 mins 2.      伍 ...

  3. CentOS 6.4 安装思维到图工具TheBrain

    最近学习中需要使用思维导图的工具,但是使用的系统是CentOS,在网上找到了一个比较好的思维导图工具:TheBrain,安装完成后还是汉化版的不错啊,由于用的是linux系统,还没有找到合适的截图软件 ...

  4. Oracle课堂实验一“表的使用”代码。

    --创建本地管理表空间CustomerTBSCREATE TABLESPACE CustomerTBS         DATAFILE 'd:\Oracle11\product\11.2.0\ora ...

  5. C++相关资源

    http://www.cnblogs.com/xi52qian/p/4186983.html语言ISO/IEC JTC1/SC22/WG21 - The C++ Standards Committee ...

  6. PHP程序的一次重构记录

    项目和新需求: 我们有一个PHP写的webmail系统,有一个mail_list.php用于展现用户的邮件列表这个页面支持folderId参数(因为邮件是存在不同的文件夹下的)由于邮件太多所以支持翻页 ...

  7. delphi xe5 android 开发实现手机打电话和发短信

    转载自  http://www.raysoftware.cn/ 其实都可以通过intent和URI调用系统功能.Windows程序员可以理解成是ShellExecute.这个是万金油.可以有调用各种功 ...

  8. 15 个响应式的 jQuery 图像滑块插件

    设计师和开发人员总是试图使用新技术让网站更智能,而我们发现在许多网站上 jQuery 的图像滑块插件是非常受欢迎的.本文继续介绍 15 个 jQuery 图像滑块插件以供您选择. ELASTISLID ...

  9. Android开源项目发现---ListView篇(持续更新)

    资料转载地址:https://github.com/Trinea/android-open-project 1. android-pulltorefresh 一个强大的拉动刷新开源项目,支持各种控件下 ...

  10. Markdown各种小问题汇总

    如何分割Quote? How can I write two separate blockquotes in sequence using markdown? > Imagination is ...