KEMBAR78
MEDIUM MAXIMUM SUBARRAY Leetcode Step by Step | PPTX
WEEK 08 MEDIUM
MAXIMUM SUBARRAY
Leetcode Problem: 53
Problem Statement
 Kadane's Algorithm
 Given:
 An integer array nums of length n (including negative and positive
numbers).
 Task:
 Find the subarray (containing at least one element) that has the
maximum sum, and return this maximum sum.
Problem Statement
 Example 1
 Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
 Output: 6
 Explanation: The subarray [4,-1,2,1] has the largest sum 6.
 Example 2:
 Input: nums = [1]
 Output: 1
 Explanation: The subarray [1] has the largest sum 1.
 Example 3:
 Input: nums = [5,4,-1,7,8]
 Output: 23
 Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
def maxSubArray(self, nums: List[int]) -> int:
max_so_far = float('-inf')
current_max = 0
for num in nums:
current_max += num
if current_max > max_so_far:
max_so_far = current_max
if current_max < 0:
current_max = 0
return max_so_far
Python Solution
Stores the maximum sum found so far. It is
initialized to negative infinity to ensure that
any valid sum will be greater
Tracks the sum of the current subarray being
considered. It starts at 0
Iterate over each element (num) in the list nums
Add the current number (num) to the current
subarray sum (current_max). This extends the
current subarray
If the current subarray sum (current_max) is
greater than the maximum found so far,
update max_so_far
If current_max becomes negative, reset it to 0.
A negative sum means the subarray is not
contributing positively to future sums. By
resetting to 0, we start a new subarray from the
next element
At the end, max_so_far contains the largest sum
of any subarray
Example Walkthrough
def maxSubArray(self, nums: List[int]) -> int:
max_so_far = float('-inf')
current_max = 0
for num in nums:
current_max += num
if current_max > max_so_far:
max_so_far = current_max
if current_max < 0:
current_max = 0
return max_so_far
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
current_max  0
max_so_far  -inf
current_max  -2
True
max_so_far  -2
True
current_max  0
Example Walkthrough
def maxSubArray(self, nums: List[int]) -> int:
max_so_far = float('-inf')
current_max = 0
for num in nums:
current_max += num
if current_max > max_so_far:
max_so_far = current_max
if current_max < 0:
current_max = 0
return max_so_far
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
current_max  0
max_so_far  -2
True current_max  1
max_so_far  1
False
Example Walkthrough
def maxSubArray(self, nums: List[int]) -> int:
max_so_far = float('-inf')
current_max = 0
for num in nums:
current_max += num
if current_max > max_so_far:
max_so_far = current_max
if current_max < 0:
current_max = 0
return max_so_far
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
current_max  1
max_so_far  1
False
True
current_max  -2
current_max  0
Example Walkthrough
def maxSubArray(self, nums: List[int]) -> int:
max_so_far = float('-inf')
current_max = 0
for num in nums:
current_max += num
if current_max > max_so_far:
max_so_far = current_max
if current_max < 0:
current_max = 0
return max_so_far
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
current_max  0
max_so_far  1
True
False
current_max  4
max_so_far  4
Example Walkthrough
def maxSubArray(self, nums: List[int]) -> int:
max_so_far = float('-inf')
current_max = 0
for num in nums:
current_max += num
if current_max > max_so_far:
max_so_far = current_max
if current_max < 0:
current_max = 0
return max_so_far
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
max_so_far  4
False
False
current_max  4
current_max  3
Example Walkthrough
def maxSubArray(self, nums: List[int]) -> int:
max_so_far = float('-inf')
current_max = 0
for num in nums:
current_max += num
if current_max > max_so_far:
max_so_far = current_max
if current_max < 0:
current_max = 0
return max_so_far
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
max_so_far  4
True
False
current_max  3
current_max  5
max_so_far  5
max_so_far  5
Example Walkthrough
def maxSubArray(self, nums: List[int]) -> int:
max_so_far = float('-inf')
current_max = 0
for num in nums:
current_max += num
if current_max > max_so_far:
max_so_far = current_max
if current_max < 0:
current_max = 0
return max_so_far
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
max_so_far  6
True
False
current_max  5
current_max  6
Example Walkthrough
def maxSubArray(self, nums: List[int]) -> int:
max_so_far = float('-inf')
current_max = 0
for num in nums:
current_max += num
if current_max > max_so_far:
max_so_far = current_max
if current_max < 0:
current_max = 0
return max_so_far
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
False
False
current_max  6
max_so_far  6
current_max  1
Example Walkthrough
def maxSubArray(self, nums: List[int]) -> int:
max_so_far = float('-inf')
current_max = 0
for num in nums:
current_max += num
if current_max > max_so_far:
max_so_far = current_max
if current_max < 0:
current_max = 0
return max_so_far
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
False
False
current_max  1
max_so_far  6
current_max  5
Final Answer: 6
Java Solution
public int maxSubArray(int[] nums) {
int maxSoFar = Integer.MIN_VALUE;
int currentMax = 0;
for (int num : nums) {
currentMax += num;
if (currentMax > maxSoFar)
maxSoFar = currentMax;
if (currentMax < 0)
currentMax = 0;
}
return maxSoFar;
}
Stores the maximum sum found so far
Stores the sum of the current subarray
Add the current number to the current subarray
Update maxSoFar if a new max is found
Reset currentMax if it goes negative
Return the maximum subarray sum
MEDIUM MAXIMUM SUBARRAY Leetcode Step by Step
MEDIUM MAXIMUM SUBARRAY Leetcode Step by Step

MEDIUM MAXIMUM SUBARRAY Leetcode Step by Step

  • 1.
    WEEK 08 MEDIUM MAXIMUMSUBARRAY Leetcode Problem: 53
  • 2.
    Problem Statement  Kadane'sAlgorithm  Given:  An integer array nums of length n (including negative and positive numbers).  Task:  Find the subarray (containing at least one element) that has the maximum sum, and return this maximum sum.
  • 3.
    Problem Statement  Example1  Input: nums = [-2,1,-3,4,-1,2,1,-5,4]  Output: 6  Explanation: The subarray [4,-1,2,1] has the largest sum 6.  Example 2:  Input: nums = [1]  Output: 1  Explanation: The subarray [1] has the largest sum 1.  Example 3:  Input: nums = [5,4,-1,7,8]  Output: 23  Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
  • 4.
    def maxSubArray(self, nums:List[int]) -> int: max_so_far = float('-inf') current_max = 0 for num in nums: current_max += num if current_max > max_so_far: max_so_far = current_max if current_max < 0: current_max = 0 return max_so_far Python Solution Stores the maximum sum found so far. It is initialized to negative infinity to ensure that any valid sum will be greater Tracks the sum of the current subarray being considered. It starts at 0 Iterate over each element (num) in the list nums Add the current number (num) to the current subarray sum (current_max). This extends the current subarray If the current subarray sum (current_max) is greater than the maximum found so far, update max_so_far If current_max becomes negative, reset it to 0. A negative sum means the subarray is not contributing positively to future sums. By resetting to 0, we start a new subarray from the next element At the end, max_so_far contains the largest sum of any subarray
  • 5.
    Example Walkthrough def maxSubArray(self,nums: List[int]) -> int: max_so_far = float('-inf') current_max = 0 for num in nums: current_max += num if current_max > max_so_far: max_so_far = current_max if current_max < 0: current_max = 0 return max_so_far nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] current_max  0 max_so_far  -inf current_max  -2 True max_so_far  -2 True current_max  0
  • 6.
    Example Walkthrough def maxSubArray(self,nums: List[int]) -> int: max_so_far = float('-inf') current_max = 0 for num in nums: current_max += num if current_max > max_so_far: max_so_far = current_max if current_max < 0: current_max = 0 return max_so_far nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] current_max  0 max_so_far  -2 True current_max  1 max_so_far  1 False
  • 7.
    Example Walkthrough def maxSubArray(self,nums: List[int]) -> int: max_so_far = float('-inf') current_max = 0 for num in nums: current_max += num if current_max > max_so_far: max_so_far = current_max if current_max < 0: current_max = 0 return max_so_far nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] current_max  1 max_so_far  1 False True current_max  -2 current_max  0
  • 8.
    Example Walkthrough def maxSubArray(self,nums: List[int]) -> int: max_so_far = float('-inf') current_max = 0 for num in nums: current_max += num if current_max > max_so_far: max_so_far = current_max if current_max < 0: current_max = 0 return max_so_far nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] current_max  0 max_so_far  1 True False current_max  4 max_so_far  4
  • 9.
    Example Walkthrough def maxSubArray(self,nums: List[int]) -> int: max_so_far = float('-inf') current_max = 0 for num in nums: current_max += num if current_max > max_so_far: max_so_far = current_max if current_max < 0: current_max = 0 return max_so_far nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] max_so_far  4 False False current_max  4 current_max  3
  • 10.
    Example Walkthrough def maxSubArray(self,nums: List[int]) -> int: max_so_far = float('-inf') current_max = 0 for num in nums: current_max += num if current_max > max_so_far: max_so_far = current_max if current_max < 0: current_max = 0 return max_so_far nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] max_so_far  4 True False current_max  3 current_max  5 max_so_far  5
  • 11.
    max_so_far  5 ExampleWalkthrough def maxSubArray(self, nums: List[int]) -> int: max_so_far = float('-inf') current_max = 0 for num in nums: current_max += num if current_max > max_so_far: max_so_far = current_max if current_max < 0: current_max = 0 return max_so_far nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] max_so_far  6 True False current_max  5 current_max  6
  • 12.
    Example Walkthrough def maxSubArray(self,nums: List[int]) -> int: max_so_far = float('-inf') current_max = 0 for num in nums: current_max += num if current_max > max_so_far: max_so_far = current_max if current_max < 0: current_max = 0 return max_so_far nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] False False current_max  6 max_so_far  6 current_max  1
  • 13.
    Example Walkthrough def maxSubArray(self,nums: List[int]) -> int: max_so_far = float('-inf') current_max = 0 for num in nums: current_max += num if current_max > max_so_far: max_so_far = current_max if current_max < 0: current_max = 0 return max_so_far nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] False False current_max  1 max_so_far  6 current_max  5 Final Answer: 6
  • 14.
    Java Solution public intmaxSubArray(int[] nums) { int maxSoFar = Integer.MIN_VALUE; int currentMax = 0; for (int num : nums) { currentMax += num; if (currentMax > maxSoFar) maxSoFar = currentMax; if (currentMax < 0) currentMax = 0; } return maxSoFar; } Stores the maximum sum found so far Stores the sum of the current subarray Add the current number to the current subarray Update maxSoFar if a new max is found Reset currentMax if it goes negative Return the maximum subarray sum