-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.cpp
More file actions
31 lines (25 loc) · 810 Bytes
/
Copy pathsolution.cpp
File metadata and controls
31 lines (25 loc) · 810 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
31
class Solution
{
public:
int maxRotateFunction(vector<int> &nums)
{
int n = nums.size();
long sum = 0; // Total sum of elements
long F = 0; // Initial rotation value F(0)
// Step 1: Calculate total sum and F(0)
for (int i = 0; i < n; i++)
{
sum += nums[i]; // accumulate total sum
F += (long)i * nums[i]; // compute F(0)
}
long result = F; // store max result
// Step 2: Use recurrence relation to compute next values
for (int k = 1; k < n; k++)
{
// Transition from F(k-1) to F(k)
F = F + sum - (long)n * nums[n - k];
result = max(result, F); // update maximum
}
return (int)result;
}
};