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 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.
Subscribe to see which companies asked this question
这个题目的关键是我们不能每次都要全部乘一遍,我们算完第一次后,就可以利用第一次的结果,来计算下一个移动后的结果
计算公式为:temp = temp - (sum - A[i]) + A[i] * (A.size() - 1);temp为上一次的结果,sum为数组的和
class Solution {
public:
int maxRotateFunction(vector<int>& A) {
long long result = ;
long long sum = ;
long long temp = ;
for (int i = ;i < A.size();++i)
{
result += A[i] * i;
sum += A[i];
}
temp = result;
for (int i = ;i < A.size();++i)
{
temp = temp - (sum - A[i]) + A[i] * (A.size() - );
if (temp > result)
result = temp;
}
return result;
}
};
LeetCode 396. Rotate Function的更多相关文章
- 【LeetCode】396. Rotate Function 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rotate-fu ...
- 【leetcode❤python】 396. Rotate Function
#-*- coding: UTF-8 -*- #超时# lenA=len(A)# maxSum=[]# count=0# while count ...
- 396. Rotate Function 移动加权求和,取最大值
[抄题]: Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by ...
- 396 Rotate Function 旋转函数
给定一个长度为 n 的整数数组 A .假设 Bk 是数组 A 顺时针旋转 k 个位置后的数组,我们定义 A 的“旋转函数” F 为:F(k) = 0 * Bk[0] + 1 * Bk[1] + ... ...
- 396. Rotate Function
一开始没察觉到0123 3012 2301 而不是 0123 1230 2301 的原因,所以也没找到规律,一怒之下brute-force.. public int maxRotateFunction ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- [LeetCode] 61. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- 【刷题笔记】LeetCode 48. Rotate Image
题意 原地顺时针翻转一个 n*n 的矩阵 图解 下面例子中用 5*5 矩阵做示例,如下图,我们要把该矩阵顺时针翻转90度,并且不能使用另外的矩阵空间来暂存数据,而是原地改变矩阵中数值. 我的想法是这样 ...
- [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 ...
随机推荐
- ubuntu14.04 下emacs 24 配置
目的: 配置emacs 24 适合编程开发 主要参考JerryZhang的配置(Emacs 简易教程) http://www.perfect-is-shit.com/emacs-simple-tuto ...
- setAttribute,,,getAttribute,,,,
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Python 中的虚拟环境
检查系统是否安装了virtualenv: $ virtualenv --version 创建虚拟环境venv(名字可以随便取,一般为venv): $ virtualenv venv 使用虚拟环境ven ...
- ubuntu12.04静态ip设置问题
由于linux知识不是学的很深,所以仅代表我自己的设置成功总结. 第一步是设置/etc/network/interfaces 增加静态ip设置 auto eth0iface eth0 inet sta ...
- 关于C语言的问卷调查(补交)
你对自己的未来有什么规划?做了哪些准备?(还是处于比较迷茫的状态:我做的准备是吧自己对计算机的兴趣提起来!) 你认为什么是学习?学习有什么用?现在学习动力如何?为什么?(学习就是学自己不会的东西:增加 ...
- Linux 下编译安装MySQL
最近在研究Mysql,当然先要把它安装在机器上才行呀.记录下操作,加深记忆,也供以后参考. 准备工作: Linux版本:Redhat Linux 6.4 Mysql版本(安装包):mysql-5.6. ...
- C++知识点
typedef typedef struct _COMSTAT { DWORD fCtsHold : ; //机构内位域的定义即变量fCtsHold占1个bit空间 DWORD fDsrHold : ...
- Unity全视角跟随鼠标右键转换视角实现——研究笔记
using UnityEngine; using System.Collections; public class CameraMove : MonoBehaviour { public Transf ...
- Extnet Direct 提交后台事件文件下载设置
App.direct.MasterData.Export(App.tfSearch.getValue(), { isUpload: true ...
- c#-1 数据结构 定义相关 界面交互数据 Model层
1.时间用Nullable<UInt32> 除了最初时间用DateTime TimeSpan不行. 2.其他元素也用Nullable<UInt32> 3.list集合数据绑定类 ...