[Leetcode] 798. Smallest Rotation with Highest Score 解题报告

这篇博客介绍了LeetCode 798题的解题报告,探讨了如何找到使数组元素大于或等于其索引的最大得分的最小旋转次数。博主提出了一种线段表示和扫描线算法,避免了暴力求解的O(n^2)时间复杂度,实现了O(nlogn)的时间复杂度和O(n)的空间复杂度的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1], A{K+2], ... A[A.length - 1], A[0], A[1], ..., A[K-1].  Afterward, any entries that are less than or equal to their index are worth 1 point. 

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].

Over all possible rotations, return the rotation index K that corresponds to the highest score we could receive.  If there are multiple answers, return the smallest such index K.

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

So we should choose K = 3, which has the highest score.

 Example 2:

Input: [1, 3, 0, 2, 4]
Output: 0
Explanation:  A will always have 3 points no matter how it shifts.
So we will choose the smallest K, which is 0.

Note:

  • A will have length at most 20000.
  • A[i] will be in the range [0, A.length].

思路

我们假设A的大小为n,如果采用暴力法逐个测试,则时间复杂度为O(n^2),应该过不了大数据测试。

我采取的方法是:首先计算使得每个元素A[i]要符合条件,需要rotate的K的集合,用线段表示;然后再扫描一遍,求出这些线段中重合最多的点,那么这个点对应的rotate次数就是题目所要计算的K。对应A[i]来讲,如果A[i] <= i,那么它向左移动到j也可能维持A[i] <= j,所以我们计算出此时它向左移动的合法区间[0, i - A[i]]。那么A[i]向右移动的合法区间是多少呢?我们知道它向右移动最多移动到n - 1,即移动n - 1 - i步;而最少需要移动max(1, A[i] - i)步,其中1表示A[i] <= 的情况。那么如果这个区间合法,就可以同样构成了一个合法的移动区间[i + 1, n - max(1, A[i] - i)]。

得到多个线段构成的合法移动区间之后,我们的任务就是求出这些区间的最大重合点。首先对segment中的各个点进行排序,然后采用扫描线的方法计算最大最大重合处。为了便于区分某个点是起点还是终点,我们定义一个pair<int, bool>来表示点,并且让起点的bool值为false,终点的bool值为true,这样就可以在扫描到某个点之后,先处理起点,再处理终点。

由于每个A[i]最多对应2个合法移动区间,所以segments大小也是O(n)量级的。这样可以得知,本算法的时间复杂度是O(nlogn),空间复杂度是O(n)。

代码

class Solution {
public:
    int bestRotation(vector<int>& A) {
        int n = A.size();
        vector<pair<int, bool>> segments;
        for (int i = 0; i < n; ++i) {
            if (A[i] <= i) {                            // we can move A[i] left
                segments.push_back(make_pair(0, false));
                segments.push_back(make_pair(i - A[i], true));
            }
            int right_move_min = max(1, A[i] - i);
            int right_move_max = n - 1 - i;
            if (right_move_min <= right_move_max) {     // we can move A[i] right to the end
                    segments.push_back(make_pair(n - right_move_max, false));
                    segments.push_back(make_pair(n - right_move_min, true));
            }
        }
        sort(segments.begin(), segments.end());
        int max_point = 0, point = 0, K = 0;
        for (int i = 0; i < segments.size(); ++i) {
            if (segments[i].second == false) {
                ++point;
                if (point > max_point) {
                    max_point = point;
                    K = segments[i].first;
                }
            }
            else {
                --point;
            }
        }
        return K;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值