add: 220422-CN [cpp]
This commit is contained in:
parent
4ab41d0c7d
commit
2d9fb8ff8b
|
|
@ -0,0 +1,37 @@
|
|||
#include <vector>
|
||||
#include <numeric>
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
|
||||
/**
|
||||
* 396. Rotate Function
|
||||
* You are given an integer array nums of length n.
|
||||
* Assume arrk to be an array obtained by rotating nums by k positions clock-wise. We define the rotation function F on nums as follow:
|
||||
* F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].
|
||||
* Return the maximum value of F(0), F(1), ..., F(n-1).
|
||||
* The test cases are generated so that the answer fits in a 32-bit integer.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
static int maxRotateFunction(const std::vector<int>& nums) {
|
||||
const int sum = std::accumulate(nums.begin(), nums.end(), 0), n = nums.size();
|
||||
int val = 0, ret = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
val += i * nums[i];
|
||||
}
|
||||
ret = val;
|
||||
std::printf("%d\n", val);
|
||||
for (int i = 1; i < n; ++i) {
|
||||
val = val - sum + n * nums[i - 1];
|
||||
std::printf("%d\n", val);
|
||||
ret = std::max(ret, val);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::printf("\n\n%d\n", Solution::maxRotateFunction({4, 3, 2, 6}));
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2204)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2204 220422.cpp)
|
||||
ADD_EXECUTABLE(2204 220422-CN.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue