15% off all plans & roadmaps Use code
See plansLeetCode 918 · Solution & editorial
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty subarray of C.
Maximum Sum Circular Subarray is a Dynamic Programming (DP) Patterns problem, in the 1D Array (Kadane's Algorithm for Max/Min Subarray) subpattern. The pattern covers fibonacci, Kadane, knapsack, LCS, LIS, edit distance, grid paths, and word break
Master Kadane's algorithm for finding maximum (or minimum) subarray sum in O(n) time.
The reference solution runs in O(n) time and O(1) space.
Maximum Sum Circular Subarray has been reported in interviews at Amazon, Bloomberg, Facebook, Google, Microsoft, Two Sigma, Uber.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Maximum Sum Circular Subarray is a Dynamic Programming (DP) Patterns problem, in the 1D Array (Kadane's Algorithm for Max/Min Subarray) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.