-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path798.cpp
More file actions
31 lines (30 loc) · 794 Bytes
/
798.cpp
File metadata and controls
31 lines (30 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
int bestRotation(vector<int>& nums) {
int n = nums.size();
vector<int> diff(n, 0);
for (int i = 0; i < n; ++i) {
if (nums[i] <= i) {
diff[0] += 1;
diff[(i - (nums[i] - 1)) % n] -= 1;
diff[(i + 1) % n] += 1;
}
else {
diff[0] += 0;
diff[(i + 1) % n] += 1;
diff[(i + 1 + n - nums[i]) % n] -= 1;
}
}
int res = 0;
int maxScore = INT_MIN;
int score = 0;
for (int i = 0; i < n; ++i) {
score += diff[i];
if (score > maxScore) {
maxScore = score;
res = i;
}
}
return res;
}
};