Description Link to heading

798. Smallest Rotation with Highest Score (Hard)

You are given an array nums. You can rotate it by a non-negative integer k so that the array becomes [nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1], ..., nums[k-1]]. Afterward, any entries that are less than or equal to their index are worth one point.

  • For example, if we have nums = [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].

Return the rotation index k that corresponds to the highest score we can achieve if we rotated nums by it. If there are multiple answers, return the smallest such index k.

Example 1:

Input: nums = [2,3,1,4,0]
Output: 3
Explanation: Scores for each k are listed below:
k = 0,  nums = [2,3,1,4,0],    score 2
k = 1,  nums = [3,1,4,0,2],    score 3
k = 2,  nums = [1,4,0,2,3],    score 3
k = 3,  nums = [4,0,2,3,1],    score 4
k = 4,  nums = [0,2,3,1,4],    score 3
So we should choose k = 3, which has the highest score.

Example 2:

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

Constraints:

  • 1 <= nums.length <= 10⁵
  • 0 <= nums[i] < nums.length

Solution Link to heading

This problem is somewhat intricate. When we perform cyclic rotations by $k$, it is equivalent to rotating the array to the left by $k$ positions. We can discuss two scenarios:

  • When $k \leq i$, after the move, $nums[i]$ will be at index $i - k$. We require that $nums[i] \leq i - k$, which means $0 \leq k \leq i - nums[i]$.
  • When $k > i$, after the move, $nums[i]$ will be at index $i - k + n$. We require that $nums[i] \leq i - k + n$, which means $i < k \leq i - nums[i] + n$.

Hence, we iterate through the $nums$ array, and for each $nums[i]$, we determine the range of movement counts $k$ that satisfy the conditions. In other words, if we have an array $a$, we need to increment $a[k] + 1$ for every valid $k$ within this range. Finally, we find the maximum $a[k]$ and its corresponding $k$, which represents the interval modification. We can consider optimizing this process using a difference array.

Code Link to heading

class Solution {
  public:
    int bestRotation(vector<int> &nums) {
        int n = nums.size();
        vector<int> diff(n);
        for (int i = 0; i < n; ++i) {
            if (i - nums[i] < 0) {
                if (i - nums[i] + n > i) {
                    if (i + 1 < n) {
                        diff[i + 1] += 1;
                    }
                    if (i - nums[i] + n < n - 1) {
                        diff[i - nums[i] + n + 1] -= 1;
                    }
                }
            } else {
                diff[0] += 1;
                if (i - nums[i] < n - 1) {
                    diff[i - nums[i] + 1] -= 1;
                }
                if (i + 1 < n) {
                    diff[i + 1] += 1;
                }
            }
        }
        int sum = diff[0], mx = diff[0];
        int res = 0;
        for (int i = 1; i < n; ++i) {
            sum += diff[i];
            if (sum > mx) {
                res = i;
                mx = sum;
            }
        }
        return res;
    }
};