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

相关文章
|
2月前
【bug记录】旋转链表与力扣报错:member access within null pointer of type ‘struct ListNode‘
【bug记录】旋转链表与力扣报错:member access within null pointer of type ‘struct ListNode‘
|
2月前
|
存储 算法
LeetCode第48题旋转图像
LeetCode第48题"旋转图像"的解题方法,通过两次翻转操作——先水平翻转再对角线翻转,实现了原地旋转矩阵的效果。
LeetCode第48题旋转图像
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 11. 旋转数组的最小数字
解决剑指Offer 11题 "旋转数组的最小数字" 的三种Python实现方法:直接使用min函数、线性查找分界点和二分查找法,以找出旋转数组中的最小元素。
36 2
|
4月前
|
C语言
详解Leetcode中关于malloc模拟开辟二维数组问题,涉及二维数组的题目所给函数中的各个参数的解读
详解Leetcode中关于malloc模拟开辟二维数组问题,涉及二维数组的题目所给函数中的各个参数的解读
27 1
|
4月前
|
存储 机器学习/深度学习 算法
python 五种算法转置后翻转、层次旋转、递归分块、一次性旋转、环状替换 实现旋转图像【力扣题48】
python 五种算法转置后翻转、层次旋转、递归分块、一次性旋转、环状替换 实现旋转图像【力扣题48】
|
4月前
|
存储 算法 数据可视化
深入解读力扣154题:寻找旋转排序数组中的最小值 II(多种方法及详细ASCII图解)
深入解读力扣154题:寻找旋转排序数组中的最小值 II(多种方法及详细ASCII图解)
|
4月前
|
存储 算法 数据可视化
|
4月前
|
机器学习/深度学习 存储
力扣经典150题第三十六题:旋转图像
力扣经典150题第三十六题:旋转图像
25 0
|
4月前
【LeetCode刷题】二分查找:寻找旋转排序数组中的最小值、点名
【LeetCode刷题】二分查找:寻找旋转排序数组中的最小值、点名
|
4月前
|
算法
【经典LeetCode算法题目专栏分类】【第6期】二分查找系列:x的平方根、有效完全平方数、搜索二位矩阵、寻找旋转排序数组最小值
【经典LeetCode算法题目专栏分类】【第6期】二分查找系列:x的平方根、有效完全平方数、搜索二位矩阵、寻找旋转排序数组最小值