Skip to content

Maximum Sum Circular Subarray

MediumGreedy
Decode it first. Clues, constraints, pattern and plan before any code.

nums is circular: after the last element comes the first one again. Return the largest sum of a non-empty run of neighbouring elements. A run may wrap around the end, but it may use each element at most once.

nums = [1, -2, 3, -2]   →  3    (just [3])
nums = [5, -3, 5]       →  10   (5, then wrap to 5)
nums = [-3, -2, -3]     →  -2   (you must take at least one)

Constraints

  • n == nums.length
  • 1 <= n <= 3 * 10^4
  • -3 * 10^4 <= nums[i] <= 3 * 10^4