当前位置:网站首页>Leetcode daily question (396. rotate function)
Leetcode daily question (396. rotate function)
2022-07-20 20:51:00 【wangjun861205】
You are given an integer array nums of length n.
Assume arrk to be an array obtained by rotating nums by k positions clock-wise. We define the rotation function F on nums as follow:
F(k) = 0 _ arrk[0] + 1 _ arrk[1] + … + (n - 1) * arrk[n - 1].
Return the maximum value of F(0), F(1), …, F(n-1).
The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Input: nums = [4,3,2,6]
Output: 26
Explanation:
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
So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.
Example 2:
Input: nums = [100]
Output: 0
Constraints:
- n == nums.length
- 1 <= n <= 105
- -100 <= nums[i] <= 100
First of all, calculate the sum, Then calculate the value when the rotation operation is not performed curr, Each subsequent rotation operation takes the last one to the first , Because the coefficient in the first place is 0, So we subtract the value of the last digit by the coefficient given to it before the rotation length-1, therefore curr = curr - nums[last] _ (length-1), Then except for the last one , The coefficients of other numbers have increased 1, We calculated earlier sum Yes, all numerical coefficients are 1 And , So we just need to subtract the last number to be the increase , curr = curr + sum - nums[last], So the final , curr = curr - nums[last] _ (length-1) + sum - nums[last]
impl Solution {
pub fn max_rotate_function(nums: Vec<i32>) -> i32 {
let sum = nums.iter().map(|v| *v).sum::<i32>();
let mut curr = nums.iter().enumerate().map(|(i, v)| i as i32 * v).sum::<i32>();
let mut ans = curr;
let length = nums.len() as i32;
for v in nums.into_iter().rev() {
curr = curr - v * (length - 1) + sum - v;
ans = ans.max(curr);
}
ans
}
}
边栏推荐
- LLVM pass pwn 入门 (3)
- 《Reinforcement based mobile robot navigation in dynamic environment》翻译
- Kernel Pwn 入门 (4)
- 面试官:解释一下 ThreadLocal 核心原理
- 4. Figure network classification
- golang面试-代码编写题1-14
- Two methods of selecting objects in CAD frame, AutoCAD -- deleting duplicate line segments
- Pycharm配置PyQt5
- 数据分析就要用这款低代码报表工具
- Detailed explanation of SQL injection Foundation
猜你喜欢
随机推荐
2022-7-13总结
SQL注入基础详解
Golang interview - code writing questions 1-14
Huawei 520 million, Xinhua 3 440 million, Inspur 240 million, Lenovo 230 million, Shenxin 200 million, Dell 170 million, smartx 70 million, dawn 60 million
匿名内部类使用的局部变量要用final修饰
4. Figure network classification
Vs+Qt 界面添加背景图的两种方式(非常实用)
JSON.toJSONString无法传递boolean
纯国产!紫光SSD开始批量出货!
Opencv learning materials sharing: Chinese, graphics and text, code notes are both abundant, and it is recommended to collect them
The server automatically preempts the GPU running program
Icml2022 tutorial | causal fairness analysis, 68 Pages pdf
1. Figure introduction to machine learning Basics
面试官:解释一下 ThreadLocal 核心原理
[C语言]自定义类型(结构体~枚举~联合体)
Vs + Qt 界面设计常用函数合集
【微信小程序】label(86/100)
[liunx] wait function and waitpid function
Li Kou classic binary tree topic
Rust 中的所有权——Rust语言小小白的入门学习11