Max Rotate Function

salahelhossiny

SalahElhossiny

Posted on August 23, 2022

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
💖 💪 🙅 🚩
salahelhossiny
SalahElhossiny

Posted on August 23, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Distribute Candies
python Distribute Candies

May 13, 2023

Third Maximum Number
python Third Maximum Number

August 30, 2022

Max Rotate Function
python Max Rotate Function

August 23, 2022

Number of Islands
python Number of Islands

August 23, 2022