DEV Community

SalahElhossiny
SalahElhossiny

Posted on Edited on

Max 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(object):
    def maxRotateFunction(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        res = []
        n = len(nums)
        mx = 0

        def rotateFn(k):
            total = 0
            nonlocal nums
            nums =  nums[0:k % n-1] + nums[k+1 % n -1:]
            for i in range(n):
                total += nums[i] * i

            return total

        for i in range(n):
            mx = max(mx, rotateFn(i))
        return mx


Enter fullscreen mode Exit fullscreen mode

Top comments (0)