Maximum Sum Circular Subarray
LeetCode 918 · मध्यम · समाधान, intuition और कदम-दर-कदम वॉकथ्रू
answer = max(kadane(nums), total − min_subarray(nums)), unless everything is negative.
मैंने Maximum Sum Circular Subarray (LeetCode 918) ऐसे हल किया — Kadane / Subarray DP पैटर्न का एक मध्यम प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।
नीचे का विवरण अंग्रेज़ी में है।
Intuition
A best circular subarray either sits in the middle like a normal subarray, or it wraps around the ends. A wrapping subarray is the whole array minus some middle piece — so to maximise it, remove the minimum-sum middle subarray. Kadane gives both answers; the minimum comes from running Kadane on the negated array.
Approach
- 1
Run Kadane for the normal (non-wrapping) maximum.
- 2
If that maximum is negative, every element is negative — return it (the wrap formula would give an empty array).
- 3
Run Kadane on the negated array; its result is −(minimum subarray sum).
- 4
The wrapping candidate is total − minimum; return the larger candidate.
मैंने कैसे हल किया
Kadane twicePythonO(n) time · O(n) space
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
max_sum = float("-inf")
cur_sum =0
for num in nums:
cur_sum+=num
max_sum = max(max_sum,cur_sum)
if cur_sum<0:
cur_sum=0
return max_sum
def maxSubarraySumCircular(self, nums: List[int]) -> int:
#max(max_sum_non_cricular, total_sum-minimum sum of the subarray)
#get the max of the non circular sub array
#if all negative then it's the maximum [-1,-2,-3,-5,-4] =>-15
#if not then find the minuimum sum of the circular sub array to keep the other part maximum
#so if we can get the min and substract from the total that's the maximum
min_sum_non_circular = self.maxSubArray(nums)
if min_sum_non_circular<0:
return min_sum_non_circular
minimum_sum_circular = self.maxSubArray([-num for num in nums])
return max(min_sum_non_circular,sum(nums)-(-minimum_sum_circular))Maximum Sum Circular Subarray — अक्सर पूछे जाने वाले प्रश्न
Maximum Sum Circular Subarray के पीछे intuition क्या है?
A best circular subarray either sits in the middle like a normal subarray, or it wraps around the ends. A wrapping subarray is the whole array minus some middle piece — so to maximise it, remove the minimum-sum middle subarray. Kadane gives both answers; the minimum comes from running Kadane on the negated array.
Maximum Sum Circular Subarray (LeetCode 918) कैसे हल करें?
1. Run Kadane for the normal (non-wrapping) maximum. 2. If that maximum is negative, every element is negative — return it (the wrap formula would give an empty array). 3. Run Kadane on the negated array; its result is −(minimum subarray sum). 4. The wrapping candidate is total − minimum; return the larger candidate.
Maximum Sum Circular Subarray की time और space complexity क्या है?
Time: O(n)। Space: O(n) for the negated copy (O(1) possible)। (Accepted approach: Kadane twice।)
Maximum Sum Circular Subarray में आम गलतियाँ क्या हैं?
All-negative input: total − min would be 0 (empty subarray), which isn't allowed.