LeeCode 798. Smallest Rotation with Highest Score

题意

Example 1:
Input: [2, 3, 1, 4, 0]
Output: 3
Explanation:
Scores for each K are listed below:
K = 0, A = [2,3,1,4,0], score 2
K = 1, A = [3,1,4,0,2], score 3
K = 2, A = [1,4,0,2,3], score 3
K = 3, A = [4,0,2,3,1], score 4
K = 4, A = [0,2,3,1,4], score 3
给定一个数组,每次可以讲前k个数摞到后面去,和上面一样。
对于每个数组,其score是值比下下标小的个数。
For example, if we have [2, 4, 1, 3, 0], and we rotate by K = 2, it becomes [1, 3, 0, 2, 4]. This is worth 3 points because 1 > 0 [no points], 3 > 1 [no points], 0 <= 2 [one point], 2 <= 3 [one point], 4 <= 4 [one point].

思路

那么我们初始化的时候 就用值减去下标。
[2,3,1,4,0]的话就是[2,2,-1,1,-4]
我们遍历k就是将其分为两部分,比方说k=1的时候
就是
[2] 和 [2,-1,1,-4]
分别统计其中小于A.size()- K 和 小于 -K的个数即是当前k的score,然后不断跟新就好,线段树或者树状数组都可以做。

代码

int sum[40010];
int N;

inline int lowbit(int x) {
    return x & (-x);
}

void updata(int x, int val) {
    while (x <= N) {
        sum[x] += val;
        x += lowbit(x);
    }
}

int getsum(int x) {
    int ans = 0;
    while (x > 0) {
        ans += sum[x];
        x -= lowbit(x);
    }
    return ans;
}


class Solution {
public:
    int bestRotation(vector<int>& A) {


        N = 40000;
        vector<int> ans1, ans2;

        memset(sum, 0, sizeof(sum));
        for (int i = 0; i < A.size(); i++) {
            updata(20000 + A[i] - i, 1);
        }
        for (int i = 0; i < A.size(); i++) {
            ans1.push_back(getsum(20000 - i));
            updata(20000 + A[i] - i, -1);
        }

        memset(sum, 0, sizeof(sum));
        for (int i = 0; i < A.size(); i++) {
            ans2.push_back(getsum(20000 + A.size()- i));
            updata(20000 + A[i] - i, +1);
        }

        int ans = ans1[0] + ans2[0], index = 0;
        for (int i = 1; i < A.size(); i++) {
            if (ans1[i] + ans2[i] > ans) {
                ans = ans1[i] + ans2[i];
                index = i;
            }
        }
        return index;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值