概述
题目:
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 most20000
.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;
}
};
最后
以上就是高高航空为你收集整理的[Leetcode] 798. Smallest Rotation with Highest Score 解题报告的全部内容,希望文章能够帮你解决[Leetcode] 798. Smallest Rotation with Highest Score 解题报告所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复