LeetCode——396. 旋转函数

简介: LeetCode——396. 旋转函数

396. 旋转函数


题目描述

答案

我的代码

1、暴力法

2、规律迭代法

官方答案

迭代


题目描述


给定一个长度为 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、规律迭代法


1.png

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)。仅使用常数空间。

相关文章
|
6天前
|
机器学习/深度学习 C语言
LeetCode | 17.04.消失的数字和189.旋转数组
17.04.消失的数字 OJ链接 这里题目要求在时间复杂度上O(n)我们介绍三种方法,看看哪种方法适合这道题~~
|
6天前
|
机器学习/深度学习
leetcode代码记录(旋转图像
leetcode代码记录(旋转图像
10 0
|
6天前
|
机器学习/深度学习 人工智能 算法
LeetCode刷题--- 面试题 01.07. 旋转矩阵(原地旋转+翻转替旋转)
LeetCode刷题--- 面试题 01.07. 旋转矩阵(原地旋转+翻转替旋转)
|
6天前
|
存储 算法
LeetCode刷题--- 61. 旋转链表(快慢指针+闭合为环)
LeetCode刷题--- 61. 旋转链表(快慢指针+闭合为环)
|
6天前
|
Go
golang力扣leetcode 48.旋转图像
golang力扣leetcode 48.旋转图像
18 0
|
6天前
|
Go
golang力扣leetcode 396.旋转函数
golang力扣leetcode 396.旋转函数
25 1
|
6天前
|
Go
golang力扣leetcode 81.搜索旋转排序数组II
golang力扣leetcode 81.搜索旋转排序数组II
19 0
|
6天前
|
Go
golang力扣leetcode 33.搜索旋转排序数组
golang力扣leetcode 33.搜索旋转排序数组
16 0
|
6天前
|
Go
golang力扣leetcode 154.寻找旋转排序数组中的最小值II
golang力扣leetcode 154.寻找旋转排序数组中的最小值II
18 0
|
6天前
leetcode-61:旋转链表
leetcode-61:旋转链表
23 0