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.