前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 0396 - Rotate Function

LeetCode 0396 - Rotate Function

作者头像
Reck Zhang
发布2021-08-11 11:08:21
1350
发布2021-08-11 11:08:21
举报
文章被收录于专栏:Reck ZhangReck Zhang

Rotate Function

Desicription

Given an array of integers A and let n to be its length.

Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a “rotation function” F on A as follow:

F(k) = 0 * Bk[0] + 1 * Bk[1] + … + (n-1) * Bk[n-1].

Calculate the maximum value of F(0), F(1), …, F(n-1).

Note: n is guaranteed to be less than 105.

代码语言:javascript
复制
Example:

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

So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.

Solution

代码语言:javascript
复制
class Solution {
public:
    long long maxRotateFunction(const std::vector<int>& A) {
        long long res = LONG_LONG_MIN;
        for(int i = 0; i < A.size(); i++) {
            long long sum = 0;
            for(int index = i, count = 0; count < A.size(); index = (index + 1) % A.size(), count++) {
                sum += A[index] * count;
            }
            res = std::max(res, sum);
        }
        return A.empty() ? 0 : res;
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Rotate Function
    • Desicription
      • Solution
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档