Java Clear Code
class Solution {\n    public int bestRotation(int[] A) {\n        int n = A.length;\n        int[] a = new int[n];  // to record interval start/end\n        for
class Solution {\n    public int bestRotation(int[] A) {\n        int n = A.length;\n        int[] a = new int[n];  // to record interval start/end\n        for (int i = 0; i < A.length; i++) {\n            a[(i + 1) % n]++;             // interval start\n            a[(i + 1 - A[i] + n) % n]--;  // interval end\n        }\n        int count = 0;\n        int maxCount = -1;\n        int res = 0;\n        for (int i = 0; i < n; i++) {  // find most overlap interval\n            count += a[i];\n            if (count > maxCount) {\n                res = i;\n                maxCount = count;\n            }\n        }\n        return res;\n    }\n}
[]