Problem Statement:
- LeetCode Number: 53
- Difficulty Level: medium
- Question: Find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Input: An array of integers.
- Output: An integer representing the maximum sum of a contiguous subarray.
- Example: Input: [-2, 1, -3, 4, -1, 2, 1, -5, 4], Output: 6 (The contiguous subarray [4, -1, 2, 1] has the largest sum = 6).
Questions to be Asked to the Interviewer:
- Can the array be empty? If so, what should be the return value?
- Is it guaranteed that there will be at least one positive integer in the array?
Edge Cases for This Problem:
- Empty Array: If the array is empty, there's no subarray to consider.
- All Negative Numbers: If all numbers are negative, the maximum subarray may contain only the least negative number.
Available Approaches to Solve This Problem:
- Brute Force: Iterate through all possible subarrays, calculating the sum for each, and find the maximum. (Time complexity: O(n2)
- O(n2))
- Kadane's Algorithm: A more efficient approach using dynamic programming.
My Approach to Solve This Problem:
- I will use Kadane's Algorithm to solve this problem, as it is more efficient.
- Maintain two variables, max_sum and current_sum. Iterate through the array, adding each element to current_sum, and update max_sum whenever current_sum is greater.
- If current_sum becomes negative, reset it to zero.
Algorithm and Data Structure:
- Algorithm: Dynamic Programming
- Data Structure: Array
def maxSubArray(nums):
? ? max_sum = current_sum = nums[0]
? ? for num in nums[1:]:
? ? ? ? current_sum = max(num, current_sum + num)
? ? ? ? max_sum = max(max_sum, current_sum)
? ? return max_sum
Explain the Solution to a Non-Programmer:
- Imagine you are walking along a path that goes up and down hills, and you want to find the most beautiful part of the path to take a photo.
- You start walking and keep track of how beautiful the path is at each step, noting if it's getting better or worse.
- If the beauty starts to decline (goes negative), you start looking again from that point to find a new beautiful part.
- In the end, you find the most beautiful part of the path where you will take the photo.
- In our problem, the path's beauty represents the sum of the numbers in the array, and we are looking for the most beautiful part, or the largest sum.
Code in Detail:
- Initialization: Start with max_sum and current_sum equal to the first element of the array. These variables will keep track of the maximum sum found so far and the sum of the current subarray, respectively.
- Iterate through the Array: Loop through the numbers in the array, starting from the second element:Update Current Sum: For each number, add it to current_sum. If this sum becomes negative, reset current_sum to the current number itself. This means we're starting to look at a new subarray.
- Update Maximum Sum: If the current_sum is greater than max_sum, update max_sum with the value of current_sum. This ensures that we always have the largest sum found so far.
- Return the Result: Finally, max_sum will hold the largest sum of any contiguous subarray, and we return this value.
The given approach smartly handles the array by keeping track of the sum of the current subarray and the maximum sum found, ensuring an efficient way to find the maximum sum of any contiguous subarray.
Pattern of this Problem
This problem falls into the pattern of "Kadane's Algorithm," a widely used technique to solve various array-related problems, especially dealing with subarrays.
Big O Notation
The time complexity of this solution is O(n)
O(n), where n is the length of the array. This is because we iterate through the entire array once. The space complexity is O(1)
O(1) since we only use a constant amount of extra space.
Points to Remember to Solve this Problem :
- Initialize the current and maximum sum as the first element of the array.
- Iterate through the array and keep track of the current sum, resetting if it falls below the current number.
- Update the maximum sum whenever a larger current sum is found.
- This problem can be solved using Kadane's Algorithm.
Code Line by Line with Some Sample Data
Let's consider an array [-2, 1, -3, 4, -1, 2, 1, -5, 4].
- Initialize max_sum = -2 and current_sum = -2.
- First Iteration: current_sum = 1, max_sum = 1.
- Second Iteration: current_sum = -2, max_sum = 1.
- Third Iteration: current_sum = 4, max_sum = 4.
- Continue similarly till the end.
- Final max_sum = 6, which is the answer.
Key Syntax Used in this Solution:
- Looping through the array: for i in range(1, len(nums)):.
- Updating values with conditions: current_sum = max(current_sum + nums[i], nums[i]).
- Conditional assignments: max_sum = max(max_sum, current_sum).
This is the end of the week 2 problems.