[LeetCode] Rotate Function 旋转函数
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
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Rotate Function 旋转函数的更多相关文章
- 396 Rotate Function 旋转函数
给定一个长度为 n 的整数数组 A .假设 Bk 是数组 A 顺时针旋转 k 个位置后的数组,我们定义 A 的“旋转函数” F 为:F(k) = 0 * Bk[0] + 1 * Bk[1] + ... ...
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- [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 ...
- [Leetcode] rotate image 旋转图片
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 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 String 旋转字符串
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- [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】396. Rotate Function 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rotate-fu ...
- LeetCode 61:旋转链表 Rotate List
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...
随机推荐
- [转] 给ubuntu中的软件设置desktop快捷方式(以android studio为例)
原文链接:http://www.cnblogs.com/kinyoung/p/4493472.html ubuntu的快捷方式都在/usr/share/applications/路径下有很多*.des ...
- 把UI图里的小图标制作成icon font
一个交互比较多的UI图里面可能会有很多小图标,一般可用sprites图将多个小图标弄成一张大图,或者其它的办法,各种方法的比较可参见博主的另外一篇博客使用css3新属性clip-path制作小图标,本 ...
- java获取https网站证书,附带调用https:webservice接口
一.java 获取https网站证书: 1.创建一个java工程,新建InstallCert类,将以下代码复制进去 package com; import java.io.BufferedReader ...
- python学习笔记(基础三:if else流程判断、while循环、for循环)
if else流程判断 getpass在pycharm中无法使用,在命令行窗口中进入python环境可以使用. import getpassusername = input("usernam ...
- Delphi_02_Delphi程序的结构
一.工程文件 program MultiUnit; {$APPTYPE CONSOLE} uses SysUtils, Unit1 in 'Unit1.pas'; begin //引用unit1中的变 ...
- Windows安装RabbitMQ集群的几个注意点
记录一下RabbitMQ在windows平台下安装的几个注意点- -,好记性不如烂笔头 安装过程与Linux安装一致,教程参照官网集群配置:此处只列举出几个注意点: 1. erlang的版本需要一致, ...
- observejs改善组件编程体验
传送门 observejs:https://github.com/kmdjs/observejs 本文演示:http://kmdjs.github.io/observejs/list/ 本文代码:ht ...
- ngrok
为什么要使用ngrok?/ngrok 作为一个Web开发者,我们有时候会需要临时地将一个本地的Web网站部署到外网,以供它人体验评价或协助调试等等,通常我们会这么做: 找到一台运行于外网的Web服务器 ...
- Apache Spark 1.6 Hadoop 2.6 Mac下单机安装配置
一. 下载资料 1. JDK 1.6 + 2. Scala 2.10.4 3. Hadoop 2.6.4 4. Spark 1.6 二.预先安装 1. 安装JDK 2. 安装Scala 2.10.4 ...
- Undefined symbols for architecture arm64解决方案
在iOS开发中经常遇到的一个错误是Undefined symbols for architecture arm64,这个错误表示工程某些地方不支持arm64指令集.那我们应该怎么解决这个问题了?我们不 ...