Sliding Window Keep Multiset
\nfrom sortedcontainers import SortedList\n\nclass Solution:\n    def bestRotation(self, nums: List[int]) -> int:\n        n = len(nums)\n        res = (0, floa
```\nfrom sortedcontainers import SortedList\n\nclass Solution:\n    def bestRotation(self, nums: List[int]) -> int:\n        n = len(nums)\n        res = (0, float(\'inf\'))\n        vals = SortedList()\n        for i in range(2 * n):\n            vals.add(i - nums[i % n])\n            if i >= n:\n                vals.remove(i - n - nums[(i - n) % n])\n            if i >= n - 1:\n                res = max(res, (len(vals) - vals.bisect_left(i - n + 1), n - i - 1))\n        return -res[1]\n```
['Python']