Skip to content

Commit ada43cb

Browse files
authored
Update 798-smallest-rotation-with-highest-score.js
1 parent 044aca2 commit ada43cb

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

798-smallest-rotation-with-highest-score.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,22 @@ const bestRotation = function(A) {
2828

2929
// another
3030

31-
const bestRotation = function(A) {
32-
let n = A.length;
33-
let c = new Array(n).fill(0);
34-
for(let i = 0; i < n; i++) {
35-
c[(i - A[i] + 1 + n) % n] -= 1;
36-
}
37-
let max = 0;
38-
for(let i = 1; i < n; i++) {
39-
c[i] += c[i-1] + 1;
40-
max = c[i] > c[max] ? i : max;
41-
}
42-
return max;
43-
}
31+
/**
32+
* @param {number[]} nums
33+
* @return {number}
34+
*/
35+
var bestRotation = function(nums) {
36+
const n = nums.length
37+
const arr = Array(n).fill(0)
38+
for(let i = 0; i < n; i++) {
39+
arr[(i - nums[i] + 1 + n) % n] -= 1
40+
}
41+
let res = 0
42+
for(let i = 1; i < n; i++) {
43+
arr[i] += arr[i - 1] + 1
44+
if(arr[i] > arr[res]) res = i
45+
}
46+
return res
47+
};
48+
49+

0 commit comments

Comments
 (0)