资讯专栏INFORMATION COLUMN

leetcode396. Rotate Function

yintaolaowanzi / 523人阅读

摘要:题目要求代表对数组在位置上进行顺时针的旋转后生成的数组。暴力循环按照题目的要求,执行两次循环即可以获得的所有值,只需要从中比较最大值即可。

题目要求
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.

Bk代表对数组A在位置k上进行顺时针的旋转后生成的数组。F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1],要求返回获得的最大的F(k)的值。

暴力循环

按照题目的要求,执行两次循环即可以获得F(k)的所有值,只需要从中比较最大值即可。

    public int maxRotateFunction(int[] A) {
        if(A == null || A.length == 0) return 0;
        int max = Integer.MIN_VALUE;
        for(int i = 0 ; i < A.length ; i++) {
            int value = 0;
            for(int j = 0 ; i < A.length ; j++) {
                value += j * A[(j+i)%A.length];
            }
            max = Math.max(value, max);
        }
        return max;
    }
数学思路
F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]
F(k-1) = 0 * Bk-1[0] + 1 * Bk-1[1] + ... + (n-1) * Bk-1[n-1] 

F(k) = F(k-1) + sum - n*Bk[0]

k = 0 Bk[0] = A[0]
k = 1 Bk[0] = A[len-1]
k = 2 Bk[0] = A[len-2]
...
    public int maxRotateFunction(int[] A) {
        if(A == null || A.length == 0) return 0;
        int F = 0;
        int sum = 0;
        for(int i = 0 ; i

想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

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

转载请注明本文地址:https://www.ucloud.cn/yun/72428.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
  • [LeetCode] Rotate Function

    摘要:是数组各位累加和,是按照对数组乘积变换后的累加和,是题目所求的不同变换累加和的最大值。 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 ...

    AlphaGooo 评论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条评论

yintaolaowanzi

|高级讲师

TA的文章

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