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.

F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25    [4, 3, 2, 6]

F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16      [6, 4, 3, 2]        diff = (4+3+2) - 6*(nums.length-1)

F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23          [2, 6, 4, 3]    diff = (6+4+3) - 2*(nums.length-1)

 public class Solution {
public int maxRotateFunction(int[] A) {
if (A==null || A.length==0) return 0;
int value = 0;
int sum = 0;
int maxValue = Integer.MIN_VALUE; for (int i=0; i<A.length; i++) {
value += i * A[i];
sum += A[i];
} for (int j=A.length-1; j>=0; j--) {
value = value + (sum-A[j])-A[j]*(A.length-1);
maxValue = Math.max(maxValue, value);
}
return maxValue;
}
}

Leetcode: Rotate Function的更多相关文章

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

  2. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

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

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

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

  5. 【leetcode❤python】 396. Rotate Function

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

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

  7. [LeetCode] Rotate List 旋转链表

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

  8. [LeetCode] Rotate Image 旋转图像

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

  9. LeetCode——Rotate List

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

随机推荐

  1. ArcGIS Server发布服务,报错00032

    错误00032 独立表数据源无法访问 00032: 修复已断开的数据连接,具体做法是将每个独立表的数据源都设置为正确的位置. 或者,如果不需要该表,将其从文档中移除. 建议在ArcMap中重新加载数据 ...

  2. PureBasic 集成Form设计器的使用

    The PureBasic IDE has a very powerful integrated form designer, which allows to design easily window ...

  3. circle and bar

    <!doctype html> <meta charset="utf-8"> <html> <head> <title> ...

  4. json格式的转换为json字符串函数

    function toJSON(object){ var type = typeof object; if ('object' == type) { if (Array == object.const ...

  5. 蓝牙BLE LINK LAYER剖析(一) -- status and channel

    一.LINK LAYER STATES 二.PHYSICAL CHANNEL

  6. 《linux内核设计与实现》读书笔记第五章——系统调用

    第5章 系统调用 操作系统提供接口主要是为了保证系统稳定可靠,避免应用程序恣意妄行. 5.1 与内核通信 系统调用在用户空间进程和硬件设备之间添加了一个中间层. 该层主要作用有三个: 为用户空间提供了 ...

  7. nodejs爬虫

    前言 几个月之前,有同事找我要PHP CI框架写的OA系统.他跟我说,他需要学习PHP CI框架,我建议他学习大牛写的国产优秀框架QeePHP. 我上QeePHP官网,发现官方网站打不开了,GOOGL ...

  8. jdk1.7的collections.sort(List list)排序问题

    1.7使用旧排序: System.setProperty("java.util.Arrays.useLegacyMergeSort", "true"); 1.7 ...

  9. 移动设备优先viewport

    Bootstrap 3 的设计目标是移动设备优先,然后才是桌面设备.这实际上是一个非常及时的转变,因为现在越来越多的用户使用移动设备. 为了让 Bootstrap 开发的网站对移动设备友好,确保适当的 ...

  10. Swift 学习难点笔记

    定义一个字典 let interestingNumber = [ ,,,,,], ,,,,,], ,,,,] ] var array = interestingNumber["Prime&q ...