# Leetcode 396 - Rotate Function

Note:

• By the example, it feels like there might be some relations between each state.
• Bingo! It’s a DP question.
• I soon found that by adding sum into each state and just subtract length * endNum from it, we can get the new dp[i].
• dp[i] = dp[i - 1] + sum - length * sum.

Question:

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.

Example:

Code: