当前位置:网站首页>js力扣每日一题(2022/4/22)---396.旋转函数
js力扣每日一题(2022/4/22)---396.旋转函数
2022-04-22 22:12:00 【前端小白在前进】
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 位 整数。
示例一:
输入: 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 。
示例二:
输入: nums = [100]
输出: 0
/** * @param {number[]} nums * @return {number} */
var maxRotateFunction = function(nums) {
let sum = 0
let f = 0
for(let j = 0;j<nums.length;j++) {
sum += nums[j]
f += j * nums[j]
}
let res = f
for(let i = 0;i < nums.length; i++) {
f = f - sum + nums.length*nums[i]
res = Math.max(res,f)
}
return res
};
这道题尝试使用for暴力,但是无果,要去找规律,这道题的思想就是高中的等差数列的列项相消。
版权声明
本文为[前端小白在前进]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_52040370/article/details/124355620
边栏推荐
- Reinforcement learning (practice): dqn, double dqn, dueling dqn
- [内网渗透]——VulnStack (一)
- fastadmin 跳转后限定搜索条件
- GBase 8s V8.8 SQL 指南:教程-6.1.2(3)
- 外部中断---------stm32f407zet6
- repeat_map mp.find() + mp.erase()
- Steps to build appinventor2 on the window
- Shadowcaster shadowmask map of 0 basic unityurp rendering pipeline is indistinct (code direction)
- vickers威格士比例阀的特点
- 2.57-编写程序show_short, show_long和show_double,它们分别打印类型为short, long和double的C语言对象的字节表示。请试着在几种机器上运行。
猜你喜欢

2021下半年软件设计师上午真题及答案解析

How to use opcua protocol on appinventor?
![[4.1] trigger trigger and evictor cleaner of flick window operator](/img/68/9a32ba6cc484237cd2c7015e5179be.png)
[4.1] trigger trigger and evictor cleaner of flick window operator

396. 旋转函数 / 剑指 Offer II 013. 二维子矩阵的和

Take you to understand the principle of highly flexible spark architecture

GBase 8s V8. 8 SQL Guide: Tutorial - 6.2.1 (2)

2.57-编写程序show_short, show_long和show_double,它们分别打印类型为short, long和double的C语言对象的字节表示。请试着在几种机器上运行。

scanpy find resolution

JVM性能调优1

TOOLS. INI‘does not contain a valid tool path
随机推荐
4 / 21 Kunming supplementary questions + mathematics
GBase 8s V8.8 SQL 指南:教程-6.1.2(2)
学习笔记2-0417
多线程进阶(六)----锁机制
GBase 8s V8. 8 SQL Guide: Tutorial - 6.2.1 (2)
NLP direction -- interview and written test question set (7): pre training model Bert
略谈企业信息化的规律
Lecture recording and broadcasting | subgraph matching algorithm in graph database - Zou Lei
appinventor拓展开发
[MRCTF2020]Ez_ bypass
快速计算约数的个数——从基础到高级
多线程进阶(七)----乐观锁和悲观锁
Overview of working principle and main characteristics of ATOS proportional valve
[Luogu] p1162 filling color (BFS)
删除 vector 内所有指定的元素
How to use opcua protocol on appinventor?
【 luogu】 p1162 couleur de remplissage (bfs)
PHP wechat refund certificate
Regular expression of JS
Knowledge map opening notes