Smallest Rotation With Highest Score Problem
Description
LeetCode Problem 798.
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^5
- 0 <= nums[i] < nums.length
Solution
Approach: Difference Array over the Rotations
Scoring all n rotations one by one takes O(n2) time. Instead, turn it around: for each entry, find the rotations in which it misses its point, and add those ranges up with a difference array.
After rotating by k, the entry at index i moves to index (i - k) mod n. As k grows by one, its index drops by one, wrapping from 0 to n - 1:
- At
k = iit sits at index0. Then atk = i + 1it wraps to indexn - 1, and since every value is belown, it always scores there. - So an entry with value
vmisses its point at indices0, 1, ..., v - 1, which are thevrotationsk = i - v + 1, ..., i(taken modn). An entry withv = 0never misses.
Steps:
- For each entry with
v > 0, add 1 tolost[k]forkin the cyclic range[(i - v + 1) mod n, i], using a difference array:+1at the start and-1just after the end. If the start is greater thani, the range wraps, so split it into[start, n - 1]and[0, i]. - Take prefix sums to get
lost[k]for eachk. The score of rotationkisn - lost[k]. - Return the first
kwith the smallestlost[k]. Comparing with a strict<keeps the smallestkon ties.
For nums = [2,3,1,4,0], the entry 2 at index 0 misses at k = 4 and k = 0, a range that wraps around the end.
The detail people get wrong is the wrapped range: without splitting it, the -1 lands before the +1 and the counts go wrong.
Complexity: O(n) time and O(n) space, where n is the length of nums.
class Solution:
def bestRotation(self, nums: List[int]) -> int:
n = len(nums)
# lost[k] = number of entries that earn no point after rotating by k, built as a difference array.
diff = [0] * (n + 1)
for i, value in enumerate(nums):
if value == 0:
continue # 0 <= index always holds
# Entry i misses its point exactly for k in the cyclic range [start, i] (value rotations).
start = (i - value + 1 + n) % n
if start <= i:
diff[start] += 1
diff[i + 1] -= 1
else: # the range wraps around: [start, n - 1] and [0, i]
diff[start] += 1
diff[n] -= 1
diff[0] += 1
diff[i + 1] -= 1
best_k, fewest_lost, lost = 0, n + 1, 0
for k in range(n):
lost += diff[k]
if lost < fewest_lost: # strict: keep the smallest k on ties
best_k, fewest_lost = k, lost
return best_k