发表文章

[最新] LeeCode 798. Smallest Rotation with Highest Score

hzh0000 16天前 26

题意

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;
    }
};
相关推荐
最新评论 (0)
返回
发表文章
hzh0000
文章数
400
评论数
0
注册排名
663628