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.