资讯专栏INFORMATION COLUMN

[LeetCode] Rotate Function

AlphaGooo / 872人阅读

摘要:是数组各位累加和,是按照对数组乘积变换后的累加和,是题目所求的不同变换累加和的最大值。

Problem

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.

Note

oneSum是数组A各位累加和,patternSum是按照pattern对数组乘积变换后的累加和,max是题目所求的不同变换累加和的最大值。

oneSum = 1A + 1B + 1C + 1D;
patternSum = 0A + 1B + 2C + 3D;

然后做一个递归:每次对patternSum减少oneSum,并加上当前递归对应的A[i]*len,这样做的道理是什么呢:

max = patternSum = 0A + 1B + 2C + 3D;
A[i]*len = 4A;
patternSum = -A + 0B + 1C + 2D + 4A = 0B + 1C + 2D + 3A;
max = Math.max(max, patternSum)= Math.max(0A1B2C3D, 3A0B1C2D);

也就是说,每一次循环,相当于改变一次pattern,从0123到3012,再到2301,到1230结束,取这之中的最大值返回即可。

Solution
public class Solution {
    public int maxRotateFunction(int[] A) {
        if (A == null || A.length == 0) return 0;
        int oneSum = 0, len = A.length, patternSum = 0;
        for (int i = 0; i < len; i++) {
            oneSum += A[i];
            patternSum += (A[i] * i);
        }
        int max = patternSum;
        for (int i = 0; i < len; i++) {
            patternSum += len * A[i] - oneSum;
            max = Math.max(max, patternSum);
        }
        return max;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66224.html

相关文章

  • LeetCode 189.Rotate Array

    摘要:问题描述解题思路使用数组自带的方法和方法把数组最后一个取出来加入到头部。使用数组的方法得到后个数,再用方法删去后个数,最后用方法把得到的后个数添加到数组前面。 问题描述: 189.Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, t...

    leanote 评论0 收藏0
  • leetcode396. 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 p...

    yintaolaowanzi 评论0 收藏0
  • [LintCode/LeetCode] Rotate Array

    Problem Given an array, rotate the array to the right by k steps, where k is non-negative. Example Example 1: Input: [1,2,3,4,5,6,7] and k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the r...

    chanthuang 评论0 收藏0
  • [LeetCode] 48. Rotate Image

    Problem You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image in-place, which means you have to modify the input 2D mat...

    Warren 评论0 收藏0
  • LeetCode[48] Rotate Image

    LeetCode[48] Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 复杂度O(N^2),O(1) 代码 public void ro...

    sanyang 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<