Skip to content

Commit 8fcd2fb

Browse files
authored
Create 798.Smallest-Rotation-with-Highest-Score_v1.cpp
1 parent b83fdf2 commit 8fcd2fb

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
int bestRotation(vector<int>& A)
4+
{
5+
int N = A.size();
6+
vector<int>diff(N,0);
7+
for (int i=0; i<N; i++)
8+
{
9+
if (A[i]<=i)
10+
{
11+
diff[0] += 1;
12+
diff[(i-A[i]+1)%N]-=1;
13+
diff[(i+1)%N]+=1;
14+
}
15+
else
16+
{
17+
diff[0] += 0;
18+
diff[(i+1)%N] +=1;
19+
diff[(i+1 + N-A[i]) % N] -= 1;
20+
}
21+
}
22+
23+
int sum = 0;
24+
int ret = 0;
25+
int K = 0;
26+
for (int i=1; i<N; i++)
27+
{
28+
sum += diff[i];
29+
if (sum > ret)
30+
{
31+
ret = sum;
32+
K = i;
33+
}
34+
}
35+
return K;
36+
}
37+
};

0 commit comments

Comments
 (0)