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的更多相关文章

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

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

  2. 【leetcode❤python】 396. Rotate Function

    #-*- coding: UTF-8 -*- #超时#        lenA=len(A)#        maxSum=[]#        count=0#        while count ...

  3. 396. Rotate Function 移动加权求和,取最大值

    [抄题]: Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by ...

  4. 396 Rotate Function 旋转函数

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

  5. 396. Rotate Function

    一开始没察觉到0123 3012 2301 而不是 0123 1230 2301 的原因,所以也没找到规律,一怒之下brute-force.. public int maxRotateFunction ...

  6. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

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

  8. 【刷题笔记】LeetCode 48. Rotate Image

    题意 原地顺时针翻转一个 n*n 的矩阵 图解 下面例子中用 5*5 矩阵做示例,如下图,我们要把该矩阵顺时针翻转90度,并且不能使用另外的矩阵空间来暂存数据,而是原地改变矩阵中数值. 我的想法是这样 ...

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

随机推荐

  1. python和nodejs的aes128加密对比

    之前的机器人是用python写的,有同事想改写成nodejs版,但是验证一直通不过,于是帮忙爬了一下文档. python版的代码大概是长这样: #encoding:utf8 from Crypto.C ...

  2. 基于云计算Iaas平台的ZStack

    2015年4月,一家全新的基础架构即服务的软件产品ZStack面世.ZStack的主创人员是自在海外云计算公司的中国人.ZStack是基于Java语言,结合了OpenStack和CloudStack上 ...

  3. Maven 学习-入门

    在进行Java开发的过程中,我接触到了Maven这套构建工具.所以,花费了点时间学习了一下这套构建工具,在学习过程中学习到的有关Maven的知识,在这里分享给大家. 什么是Maven 首先,在介绍Ma ...

  4. HTML5上传图片到ASP.NET.MVC

    @{ ViewBag.Title = "Home Page";} <!DOCTYPE HTML PUBLIC><html><head> < ...

  5. 开启Java博客

    已经转Java大半年了,Java知识都来自于工作,没有一个系统的学习,所以这一个多月我都在看Java的一些基本东西,准备系统性的学习下Java知识.这一个多月看的也挺多,从servlet,jsp,st ...

  6. UITabBarButton 点击失效问题

    开发过程: 在创建一个UIWindow时,直接在window上添加手势动作. 开发代码: UITapGestureRecognizer *tapRecognizer=[[UITapGestureRec ...

  7. 【P1379】天才的约数和

    来自GDOI2007,原题已不可考-- 又自己做出来了好开心,找特殊性是个关键的切入点 原题: 这天周航遇到了靳泽旭. 周航:"我是天才!" 靳泽旭:"你为什么是天才?& ...

  8. 你应该知道的那些Android小经验

    原文出处:http://jayfeng.com/ 做Android久了,就会踩很多坑,被坑的多了就有经验了,闲暇之余整理了部分,现挑选一些重要或者偏门的“小”经验做个记录. 查看SQLite日志 ad ...

  9. [原创]cocos2d-x研习录-第三阶 特性之加速度传感器

    智能手机的游戏与应用中,也经常会用到加速传感器事件来丰富用户的体验,比如飞翔的企鹅(英文AirPenguin)游戏就是通过加速度传感器来控制角色的移动和跳跃方向.下面学习Cocos2D-x中如何使用加 ...

  10. Linux监控分析实战-1

    监控概述及5个大指标 cpu mem   内存 io        磁盘交互 load     负载 Network 网络 它们之间关系是相互彼此依赖,任何一个高负载都会到导致其他指标出现问题: 网卡 ...