396 Rotate Function 旋转函数
给定一个长度为 n 的整数数组 A 。
假设 Bk 是数组 A 顺时针旋转 k 个位置后的数组,我们定义 A 的“旋转函数” F 为:
F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]。
计算F(0), F(1), ..., F(n-1)中的最大值。
注意:
可以认为 n 的值小于 105。
示例:
A = [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(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 。
详见:https://leetcode.com/problems/rotate-function/description/
C++:
class Solution {
public:
int maxRotateFunction(vector<int>& A) {
int t=0,sum=0,n=A.size();
for(int i=0;i<n;++i)
{
sum+=A[i];
t+=i*A[i];
}
int res=t;
for(int i=1;i<n;++i)
{
t=t+sum-n*A[n-i];
res=max(res,t);
}
return res;
}
};
参考:https://www.cnblogs.com/grandyang/p/5869791.html
396 Rotate Function 旋转函数的更多相关文章
- [LeetCode] Rotate Function 旋转函数
Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...
- 【LeetCode】396. Rotate Function 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rotate-fu ...
- LeetCode 396. Rotate Function
Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...
- 396. Rotate Function 移动加权求和,取最大值
[抄题]: Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by ...
- 【leetcode❤python】 396. Rotate Function
#-*- coding: UTF-8 -*- #超时# lenA=len(A)# maxSum=[]# count=0# while count ...
- 396. Rotate Function
一开始没察觉到0123 3012 2301 而不是 0123 1230 2301 的原因,所以也没找到规律,一怒之下brute-force.. public int maxRotateFunction ...
- [Swift]LeetCode396. 旋转函数 | Rotate Function
Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...
- Java实现 LeetCode 396 旋转函数
396. 旋转函数 给定一个长度为 n 的整数数组 A . 假设 Bk 是数组 A 顺时针旋转 k 个位置后的数组,我们定义 A 的"旋转函数" F 为: F(k) = 0 * B ...
- Leetcode 396.旋转函数
旋转函数 给定一个长度为 n 的整数数组 A . 假设 Bk 是数组 A 顺时针旋转 k 个位置后的数组,我们定义 A 的"旋转函数" F 为: F(k) = 0 * Bk[0] ...
随机推荐
- NIST的安全内容自动化协议(SCAP)以及SCAP中文社区简介
https://blog.csdn.net/langkew/article/details/8795530?utm_source=tuicool&utm_medium=referral
- Inversion 归并求逆元
bobo has a sequence a 1,a 2,…,a n. He is allowed to swap twoadjacent numbers for no more than k time ...
- 2017-10-02-afternoon
T1 最大值(max) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK有一本书,上面有很多有趣的OI问题.今天LYK看到了这么一道题目: 这里有一个长度 ...
- Vue.js父子通信之所有方法和数据共享
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用Hexo搭建博客
好长时间没在博客园写东西了,自己搭了一套博客,把自己的一些积累分享给大家,欢迎指正! 博客地址:http://www.baiguangnan.com 使用Hexo搭建自己的博客,可以参考这里:http ...
- 装机、做系统必备:秒懂MBR和GPT分区表
从Intel 6系列主板之后,就开始提供UEFI BIOS支持,正式支持GPT硬盘分区表,一举取代了此前的MBR分区表格式,不过为了保持对老平台的兼容,微软即使最新的Windows 10系统也继续提供 ...
- poj3511--A Simple Problem with Integers(线段树求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60441 ...
- C#的SplitPanel如何设置上下和左右
定位到Orientation属性即可
- what is Servlet Container[转载]
1 在这个博客中,我将描述一下web服务器.Servlet容器的基本概念,以及Servlet容器和jvm之间的关系.我想要证明的是Servlet容器不过就是一个java程序. 2 什么是web服务器 ...
- iOS 开发 公司开发者账号,在多台Mac上合作开发,共用一个账号和证书--图文详解
参考 导出证书申请的MAC里的Xcode的开发者账号 .developerprofile 导出的开发者账号文件.developerprofile 导出PKCS12既是.p12文件 所拷贝的资料, .d ...