> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Smallest Rotation with Highest Score

> Tested Python solution for LeetCode 798 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 798, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/smallest-rotation-with-highest-score/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 798   # by problem number
lcpy gen -s smallest_rotation_with_highest_score   # by problem name
```

## Problem

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

### Examples

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

```
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

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/smallest_rotation_with_highest_score/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/smallest_rotation_with_highest_score/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def best_rotation(self, nums: list[int]) -> int:
        n = len(nums)
        diff = [0] * (n + 1)
        for i, val in enumerate(nums):
            # nums[i] earns a point for rotation k exactly when val <= (i - k) % n,
            # which holds over the circular interval of k: [(i + 1) % n, (i - val + 1) % n)
            start = (i + 1) % n
            end = (i - val + 1) % n
            diff[start] += 1
            diff[end] -= 1
        best_k = 0
        best_score = -1
        score = 0
        for k in range(n):
            score += diff[k]
            if score > best_score:
                best_score = score
                best_k = k
        return best_k
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.