227. Maximum Sum Circular Subarray

Medium
Circular Kadane
Array

Problem

Given a circular integer array nums, return the maximum possible sum of a non-empty subarray. A circular subarray may wrap from the end of the array back to the beginning, but each array element may be used at most once.

Examples

Example 1

Input: [1,-2,3,-2]
Output: 3

Example 2

Input: [5,-3,5]
Output: 10

Example 3

Input: [-3,-2,-3]
Output: -2
Constraints

1 <= nums.length <= 30000

-30000 <= nums[i] <= 30000

Hints

?? First compute the ordinary maximum subarray sum.

?? A wrapping maximum can be obtained from the total sum minus the minimum subarray sum.

?? The all-negative case needs special handling.

Expected Complexity
Time: O(n)
Space: O(1)
Follow-up

Can you compute the maximum subarray, minimum subarray and total sum in one pass?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[1,-2,3,-2]
Expected
3