LeetCode——396. 旋转函数

题目描述

给定一个长度为 n 的整数数组 nums 。

假设 arrk 是数组 nums 顺时针旋转 k 个位置后的数组,我们定义 nums 的 旋转函数 F 为:

F(k) = 0 * arrk[0] + 1 * arrk[1] + … + (n - 1) * arrk[n - 1]
返回 F(0), F(1), …, F(n-1)中的最大值 。

生成的测试用例让答案符合 32 位 整数。

示例 1:

输入: nums = [4,3,2,6]
输出: 26
解释:
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
所以 F(0), F(1), F(2), F(3) 中的最大值是 F(3) = 26 。

示例 2:

输入: nums = [100]
输出: 0

提示:

  • n == nums.length
  • 1 <= n <= 105
  • -100 <= nums[i] <= 100

答案

我的代码

1、暴力法

class Solution {
    public int maxRotateFunction(int[] nums) {
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < nums.length; i++) {
            int he = 0;
            for (int i1 = 0; i1 < nums.length; i1++) {
                he += i1*nums[(i+i1)%nums.length];
            }
            max = Math.max(max,he);
        }
        return max;
    }
}

时间超时

2、规律迭代法

在这里插入图片描述

class Solution {
    public int maxRotateFunction(int[] nums) {
        int he = 0;
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            //先算出第一个F(0)
            max += i*nums[i];
            //算出数组所有元素的和
            he += nums[i];
        }
        //定义 y 变量 将F(0)赋值给 y
        int y = max;
        //从第二个数遍历数组
        for (int i = 1; i < nums.length; i++) {
            //将数组和赋值给x
            int x = he;
            //x 等于 he 减 遍历到的后一个元素
            x = x - nums[i-1];
            //计算 遍历到的后一个元素 * (nums.length-1) - x 的值
            int i1 = (nums.length-1) * nums[i - 1] - x;
            // F(n)= y + i1
            y = y+i1;
            // 取 max 和 F(n)较大的一个赋值给 max
            max = Math.max(max,y);
        }
        //返回答案
        return max;
    }
}

官方答案

迭代

思路

记数组 nums 的元素之和为 numSum。根据公式,可以得到:

F(0) = 0 × nums[0] + 1 × nums[1] + … + (n−1) × nums[n−1]
F(1) = 1 × nums[0] + 2 × nums[1] + … + 0 × nums[n−1] = F(0) + numSum − n × nums[n−1]
更一般地,当 1≤k<n 时,F(k) = F(k−1) + numSum − n × nums[n−k]。我们可以不停迭代计算出不同的 F(k),并求出最大值。

代码

class Solution {
    public int maxRotateFunction(int[] nums) {
        int f = 0, n = nums.length, numSum = Arrays.stream(nums).sum();
        for (int i = 0; i < n; i++) {
            f += i * nums[i];
        }
        int res = f;
        for (int i = n - 1; i > 0; i--) {
            f += numSum - n * nums[i];
            res = Math.max(res, f);
        }
        return res;
    }
}

复杂度分析

  • 时间复杂度:O(n),其中 n 是数组 nums 的长度。计算 numSum 和第一个 f 消耗 O(n) 时间,后续迭代 n−1 次 f 消耗 O(n) 时间。

  • 空间复杂度:O(1)。仅使用常数空间。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值