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.