当前位置:网站首页>力扣解法汇总396-旋转函数
力扣解法汇总396-旋转函数
2022-06-12 02:04:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给定一个长度为 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
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-function
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 如果单纯的实现逻辑很简单,但是数组的长度是10W,如果我们按照要求去计算的话,就需要10W*10W的计算量,O(n2)的时间复杂度,那么一定会超时。 * 因此核心是把时间复杂度降为O(n)级别,所以我们可以尝试去找规律。 * 这个规律就是每个不同的函数,其求和的变化是有规律的。比如在示例1中,F(0)和F(1)。 * nums = [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(1)=F(0)+(4+3+2)-3*6=16 * 所以这规律很明显了,只要每次都增加所有的数(不包含最后一个),然后减去最后一个数乘以(nums.length - 1)即可。
代码:
public class Solution396 {
public int maxRotateFunction(int[] nums) {
int f = 1;
int sum = 0;
int currentFK = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
currentFK += i * nums[i];
}
int maxFK = currentFK;
while (f < nums.length) {
int lastValue = nums[nums.length - f];
currentFK = currentFK + (sum - lastValue) - lastValue * (nums.length - 1);
maxFK = Math.max(currentFK, maxFK);
f++;
}
return maxFK;
}
}边栏推荐
- Software engineering course: Chapter 2 software problem definition and feasibility analysis after class exercises
- Design principle [Demeter's Law]
- MySQL advanced knowledge points
- 阿里云oss文件上传系统
- The establishment and introduction of the announcement module of PHP development blog system
- How can low code platforms improve cost effectiveness?
- 为什么我们要使用谷歌搜索广告?
- PHP development 09 article module deletion and article classification writing
- Graphic data analysis | data cleaning and pretreatment
- leetcodeSQL:612. Nearest distance on plane
猜你喜欢

Spiral matrix (skill)

Installing MySQL version 5.5 database for Linux (centos6)

redis集群(cluster)+哨兵模式+主从(replicas)

Knowledge points of mall development

ozzanimation-基于sse的动作系统

混泥土(地面+墙面)+ 山体裂缝数据集汇总(分类及目标检测)

程序员应该如何解决买菜难问题?手把手带你利用无障碍辅助功能快速下单抢菜

为什么我们要使用谷歌搜索广告?

消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)

ozzanimation-基於sse的動作系統
随机推荐
matplotlib. pyplot. Bar chart (II)
How to improve the advertising rating of advertising, that is, the quality score?
力扣解法汇总面试题 01.05. 一次编辑
ozzanimation-基於sse的動作系統
Linux(CentOS7)安装MySQL-5.7版本
力扣解法汇总497-非重叠矩形中的随机点
[learn FPGA programming from scratch -20]: quick start chapter - operation steps 4-2-quick use of Altera quartz II tool (Modelsim co simulation, program download to altera development board)
In 2022, the internal promotion of the "MIHA Tour" golden, silver and silver social recruitment started in April and march! Less overtime, good welfare, 200+ posts for you to choose, come and see!
php安全开放10文章列表模块的分页编写
C asynchronous programming from simple to deep (III) details awaiter
Various error reporting solutions encountered by Kali during Empire installation
LeetCode Algorithm 1791. Find the central node of the star chart
ACL2022 | DCSR:一种面向开放域段落检索的句子感知的对比学习方法
Redis cluster + sentinel mode + replicas
螺旋矩阵(技巧)
竞价广告每次点击出价多少钱是固定的吗?
力扣解法汇总668-乘法表中第k小的数
力扣解法汇总875-爱吃香蕉的珂珂
Does the virtual host have independent IP
消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)