diff --git a/cpp/2204/220422-CN.cpp b/cpp/2204/220422-CN.cpp new file mode 100644 index 0000000..beba919 --- /dev/null +++ b/cpp/2204/220422-CN.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +/** + * 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& 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; +} diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index 200503c..28fa430 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220422.cpp) +ADD_EXECUTABLE(2204 220422-CN.cpp)