↓Skip to main content
  1. LeetCode/

LeetCode 396: Rotate Function

·2 mins· ·
LeetCode Medium Array Math Dynamic-Programming
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-11-16

  • Submissions: Time Limit Exceeded → Wrong Answer ×2 → Accepted

Approach
#

Each rotation raises every coefficient by 1 except the last element’s, adding sum - nums[last]; the last element’s coefficient drops from n - 1 to 0, losing (n - 1) × nums[last]. Together F(k) = F(k - 1) + sum - n × nums[n - k]. Compute F(0) and roll the recurrence, in O(n) (Linear DP Template 1).

My Attempts
#

  • The first 2022 version computed every rotation directly, O(n²) and a TLE, and range(len(nums) - 1) skipped one rotation. The second never accumulated the recurrence into cur_sum, always starting from F(0).
  • In a 2026 rewrite the index of the element moved to the front started at a fixed 3, hard-coded from the example’s length, so other lengths failed; range(n - 1, 0, -1) passed.

Solution
#

class Solution:
    def maxRotateFunction(self, nums: List[int]) -> int:
        n, total = len(nums), sum(nums)
        cur = best = sum(i * x for i, x in enumerate(nums))
        for k in range(1, n):
            cur += total - n * nums[n - k]
            best = max(best, cur)
        return best

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • The element moved to the front at rotation k is nums[n - k]; the index must follow k.
  • Computing each rotation directly is O(n²) and times out.

Interview Explanation
#

Each rotation raises every coefficient by one except for the element moved to the front, so F(k) = F(k - 1) + sum - n × that element. I compute F(0) and roll the recurrence, keeping the maximum. Time O(n).

Related

LeetCode 313: Super Ugly Number
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 313 note: generalizing LC 264 with one pointer per prime; with a heap, avoid pushing the same number many times
LeetCode 435: Non-overlapping Intervals
·2 mins
LeetCode Medium Array Dynamic-Programming Greedy Sorting
LeetCode 435 note: minimum removals = total minus the maximum kept; sort by right end and greedily keep the one ending earliest
LeetCode 473: Matchsticks to Square
·2 mins
LeetCode Medium Array Dynamic-Programming Backtracking Bit-Manipulation
LeetCode 473 note: the total must be divisible by 4; place sticks from longest to shortest into one of four sides, skipping a side that would overflow or that equals a side already tried
LeetCode 264: Ugly Number II
·2 mins
LeetCode Medium Hash-Table Math Dynamic-Programming Heap-Priority-Queue
LeetCode 264 note: three pointers mark the next ugly numbers to multiply by 2, 3 and 5; take the minimum each time and advance every pointer that produced it
LeetCode 189: Rotate Array
·2 mins
LeetCode Medium Array Math Two-Pointers
LeetCode 189 note: take k %= n, then reverse three times: the whole array, the first k, and the last n - k, with O(1) extra space
LeetCode 204: Count Primes
·2 mins
LeetCode Medium Array Math Enumeration Number-Theory
LeetCode 204 note: the sieve of Eratosthenes, crossing out multiples of each prime p starting at p², with the outer loop only up to √n