File tree Expand file tree Collapse file tree 1 file changed +19
-13
lines changed Expand file tree Collapse file tree 1 file changed +19
-13
lines changed Original file line number Diff line number Diff line change @@ -28,16 +28,22 @@ const bestRotation = function(A) {
28
28
29
29
// another
30
30
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
+
You can’t perform that action at this time.
0 commit comments