Javascript solution O(n) Time and O(n) Space with explain
we transform the origin nums [2, 3, 1, 4, 0] to  nums[i] - i,\nit will be:  [2, 2, -1, 1, -4], and that is k = 0; the points is the count of nums[i] <= 0; which
we transform the origin nums [2, 3, 1, 4, 0] to  `nums[i] - i`,\nit will be:  [2, 2, -1, 1, -4], and that is k = 0; the points is the count of nums[i] <= 0; which is 2.\nthen we start rotate: \nput new number `nums[0] - nums.length` to the end of array, remove nums[0]. and count the valid numbers `nums[i] <= -k`.\nfor k = **1**: new number is `2 - 5 = -3`, new array is [2, -1, 1, -4, *-3*], the points is the count of nums[i] <= **-1**; which is 3;\nfor k = **2**: new number is `2 - 5 = -3`, new array is [-1, 1, -4, -3, *-3*], the points is the count of nums[i] <= **-2**; which is  3;\nfor k = **3**: new number is `-1 - 5 = -6`, new array is [1, -4, -3, -3, *-6*], the points is the count of nums[i] <= **-3**; which is  4;\nfor k = **4**: new number is `1 - 5 = -4`, new array is [-4, -3, -3, -6, *-4*], the points is the count of nums[i] <= **-4**; which is 3; \nthe max points is 4 and k = 3 is the answer.\n\nwhen we do the counting, it should be nums.length minus all the invalid numbers.\nbut we can use the points from previous array,\ncompare to the previous array, only 2 things changed, 1 new number, and the condition to get point decreased by 1.\nso compare to previous array only one number is going to be invalid, we just need to minus the count of this target number from lastPoints.\nand if the new number is valid we add 1 to currentPoints\n \nso, when k = 0, the transform the nums array, and count the points, which is 2.\nwhen k = 1, there are 0 of 0 in the previous array, and the new number -3 is below 0. the points is 2 - 0 + 1 = 3;\nwhen k = 2, there are 1 of -1 in the previous array, and the new number -3 is below -1. the points is 3 - 1 + 1 = 3; \nwhen k = 3, there are 0 of -2 in the previous array, and the new number -6 is below -2. the points is 3 - 0 + 1 = 4; \nwhen k = 4, there are 2 of -3 in the previous array, and the new number -4 is below -3. the points is 4 - 2 + 1 = 3; \n\n\n```\nfunction bestRotation(nums: number[]): number {\n  const numMap = new Map();\n\n  let currentPoints = 0;\n  for (let i = 0; i < nums.length; i++) {\n    const num = nums[i] - i;\n    if (num <= 0) {\n      currentPoints++;\n      numMap.set(num, (numMap.get(num) || 0) + 1);\n    }\n    nums[i] = num;\n  }\n\n  let k = 0;\n  let lastPoints = currentPoints;\n  let max = currentPoints;\n \n  // r = 0 means rotate until index r(includes), k = r + 1;\n  for (let r = 0; r < nums.length; r++) {\n    const num = nums[r] - nums.length;\n\t// reset currentPoints by lastPoints minus the count of target number\n    currentPoints = lastPoints - (numMap.get(-r) || 0);\n\t\n\t// num <= -k, means num <= -(r + 1), same as num < -r\n    if (num < -r) { \n\t  // if new number is valid increase currentPoints and add it to numMap \n      currentPoints++;\n      numMap.set(num, (numMap.get(num) || 0) + 1);\n    }\n\n    if (currentPoints > max) {\n      k = r + 1;\n      max = currentPoints;\n    }\n    lastPoints = currentPoints;\n  }\n\n  return k;\n}\n\n```
['TypeScript', 'JavaScript']