FunctionRacerProblemsRace

Best Rotation Score

MediumArraysPython, JavaScript, TypeScript, Java, C++

The score of a list is the sum of index * value over all its positions, with indexes starting at 0. Rotating nums moves some number of items from the end to the front, keeping their order: [1, 2, 3] can become [3, 1, 2] or [2, 3, 1], and rotating by zero positions leaves it unchanged.

Return the largest score any rotation of nums can have. An empty list has score 0.

Example 1

nums [4,3,2,6]
returns 26

The rotation [3, 4, 2, 6] scores 0*3 + 1*4 + 2*2 + 3*6 = 26, the best of the four rotations.

Example 2

nums [1,2,3]
returns 8

[1, 2, 3] scores 8, [3, 1, 2] scores 5 and [2, 3, 1] scores 5.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems