当前位置:网站首页>力扣解法汇总396-旋转函数
力扣解法汇总396-旋转函数
2022-04-22 16:39: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;
}
}
版权声明
本文为[失落夏天]所创,转载请带上原文链接,感谢
https://blog.csdn.net/AA5279AA/article/details/124341409
边栏推荐
- 华为机考题——HJ54 表达式求值
- 蓝桥杯练习019
- 小练习:二分查找及实现
- idea工具左侧树状结构不展示
- 领域驱动模型DDD(三)——使用Saga管理事务
- 抗疫神器:健康码、行程码自动识别!
- RTP packaging and unpacking of webrtc
- Gome's new actions "true selection" and "strict selection" enable multi-dimensional escort quality consumption
- DB107-ASEMI整流桥详细数据
- In 2023, Wuhan University of technology energy and power (085800) took the postgraduate entrance examination and landed. Experience guidance of predecessors in preparing for the examination
猜你喜欢

Interview:人工智能岗位面试—人工智能岗位求职之机器学习算法工程师必备知识框架结构图

Dispute over stock: Gome retail explores the way to break the situation with full retail

BACKBONE,NECK,HEAD

京东一面:子线程如何获取父线程 ThreadLocal 的值?我蒙了。。。

国产手机品牌才发现,没有国内消费者支持它们什么也不是

Times superior AC servo driver settings

【csnote】范式

Blue Bridge Cup practice 014

详述 CHARTEVENTS 检查记录表(十)

Algorithm:实现LDA的Gibbs Gauss采样(绘制多图subplot)
随机推荐
Blue Bridge Cup practice 014
In 2023, Wuhan University of technology energy and power (085800) took the postgraduate entrance examination and landed. Experience guidance of predecessors in preparing for the examination
TM of NLP: Based on gensim library, call 20newsgr to learn doc topic distribution and save it as train SVM LDA txt、test-svm-lda. txt
Blue Bridge Cup exercise 012
国产手机品牌才发现,没有国内消费者支持它们什么也不是
An analysis and treatment of abnormal growth of Oracle database table space
C ODBC loads the files of one folder into the blob column of Oracle database and downloads the blob column to another folder
Web security tool burp_ Suite installation and use tutorial (professional version)
CrashSight 常规功能&特色功能介绍
NFT 平台安全指南
接口协议之抓包分析 TCP 协议
numpy基础大全(创建、索引、常用函数)
存量之争:国美零售以全零售探索破局之道
C language selection sorting
From thinking to practice, digital transformation is the successful path of it operation
Test life | less than 2 years after graduation, 0 experience and won the 30W annual salary of a well-known Internet enterprise. How did he do it?
Agile practice | agile indicators to improve group predictability
NFT platform security guide
【csnote】范式
Calculation method of numerator and denominator of system driven by stepping servo motor for rotation angle control