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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public: int maxRotateFunction(vector<int>& nums) { long acc = 0, res = 0, tot = 0, sz = nums.size(); for(int i = 0; i < sz; i++) { acc += nums[i]; tot += i * nums[i]; } res = tot; for(int i = 0; i < sz; i++) { tot = tot + acc - sz * nums[sz - 1 - i]; res = max(tot, res); }
return res; } };
|