add: 220309-CN [cpp]

This commit is contained in:
Lam Haoyin 2022-03-09 21:24:36 +08:00
parent 2b5dc5138d
commit 64d6628873
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 40 additions and 1 deletions

39
cpp/2203/220309-CN.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <vector>
#include <cstring>
#include <iostream>
class Solution {
public:
static int bestRotation(const std::vector<int>& nums) {
int n = nums.size(), rotates[n];
std::memset(rotates, 0, sizeof rotates);
for (int i = 0; i < n; ++i)
if (nums[i] < n)
++rotates[(i - nums[i] + 1 + n) % n];
int ret = 0, retMax = 0;
for (int i = 0; i < n; ++i)
if (nums[i] <= i)
++retMax;
int now = retMax;
for (int i = 1; i <= n; ++i) {
// i -> i + 1
now -= rotates[i % n];
if (nums[i - 1] < n)
++now;
if (now > retMax) {
retMax = now;
ret = i;
}
}
return ret % n;
}
};
int main() {
std::cout << Solution::bestRotation({2,3,1,4,0}) << "\n";
std::cout << Solution::bestRotation({1,3,0,2,4});
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2203)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2203 220308-CN.cpp)
ADD_EXECUTABLE(2203 220309-CN.cpp)