Given an array of integers A and let n to be its length.

Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow:

F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1].

Calculate the maximum value of F(0), F(1), ..., F(n-1).

Note:
n is guaranteed to be less than 105.

Example:

A = [4, 3, 2, 6]

F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26 So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.

这道题是LeetCode第四次比赛的第一道题,博主第一道题就没有做出来,博主写了个O(n2)的方法并不能通过OJ的大数据集合,后来网上看大家的解法都是很好的找到了规律,可以在O(n)时间内完成。现在想想找规律的能力真的挺重要,比如之前那道Elimination Game也靠找规律,而用傻方法肯定超时,然后博主发现自己脑子不够活,很难想到正确的方法,说出来全是泪啊T.T。好了,来解题吧,我们为了找规律,先把具体的数字抽象为A,B,C,D,那么我们可以得到:

F(0) = 0A + 1B + 2C +3D

F(1) = 0D + 1A + 2B +3C

F(2) = 0C + 1D + 2A +3B

F(3) = 0B + 1C + 2D +3A

那么,我们通过仔细观察,我们可以得出下面的规律:

sum = 1A + 1B + 1C + 1D

F(1) = F(0) + sum - 4D

F(2) = F(1) + sum - 4C

F(3) = F(2) + sum - 4B

那么我们就找到规律了, F(i) = F(i-1) + sum - n*A[n-i],可以写出代码如下:

class Solution {
public:
int maxRotateFunction(vector<int>& A) {
int t = , sum = , n = A.size();
for (int i = ; i < n; ++i) {
sum += A[i];
t += i * A[i];
}
int res = t;
for (int i = ; i < n; ++i) {
t = t + sum - n * A[n - i];
res = max(res, t);
}
return res;
}
};

参考资料:

https://leetcode.com/problems/rotate-function/

https://leetcode.com/problems/rotate-function/discuss/87853/Java-O(n)-solution-with-explanation

https://leetcode.com/problems/rotate-function/discuss/87842/Java-Solution-O(n)-with-non-mathametical-explaination

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

[LeetCode] Rotate Function 旋转函数的更多相关文章

  1. 396 Rotate Function 旋转函数

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

  2. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  3. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  4. [Leetcode] rotate image 旋转图片

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

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

  6. [LeetCode] Rotate String 旋转字符串

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

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

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

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

  9. LeetCode 61:旋转链表 Rotate List

    ​给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...

随机推荐

  1. Java 快速排序两种实现

    快速排序,只要学习过编程的人肯定都听说过这个名词,但是有时候写的时候还真蒙住了,网上搜罗了下以及查阅了"introduction to algorithm",暂时找到两种实现快排的 ...

  2. 前端精选文摘:BFC 神奇背后的原理

    BFC 已经是一个耳听熟闻的词语了,网上有许多关于 BFC 的文章,介绍了如何触发 BFC 以及 BFC 的一些用处(如清浮动,防止 margin 重叠等).虽然我知道如何利用 BFC 解决这些问题, ...

  3. WebComponent魔法堂:深究Custom Element 之 面向痛点编程

    前言  最近加入到新项目组负责前端技术预研和选型,一直偏向于以Polymer为代表的WebComponent技术线,于是查阅各类资料想说服老大向这方面靠,最后得到的结果是:"资料99%是英语 ...

  4. IL接口和类的属性

    上一篇文章学习了IL的入门,接下来我们再通过两个例子来了解下类的属性.构造函数以及接口的使用 一.类的属性.构造函数 1.先看下我们要构建的类的C#代码,然后再进行IL的实现,示例代码如下: [Ser ...

  5. C#批量插入数据到Sqlserver中的四种方式

    我的新书ASP.NET MVC企业级实战预计明年2月份出版,感谢大家关注! 本篇,我将来讲解一下在Sqlserver中批量插入数据. 先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的 ...

  6. 转载一篇React native的props的用法

    注:默认值如何设置 http://www.tuicool.com/articles/uMfYv2q

  7. H5 表格标签基本使用

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. IOS 杂笔-9 (MD5 加密)

    首先是一段对MD5的简介 *出自一位大牛之手* Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护 ...

  9. C# 获取相对路径的字符串

    目录结构 father |—— subfolder1 |—— subfolder2 当前在 subfolder1, 通过相对路径的方式获取 subfolder2的路径 string path = Pa ...

  10. Appfuse:权限控制

    Appfuse的权限控制依赖于Struts的Menu机制,common下的menu.jsp是对菜单顺序的定义,详细的菜单项和菜单链接及权限再menu-config.xml中控制,如下: <Men ...