53.maximum-sum-subarray-en

Problem

https://leetcode.com/problems/maximum-subarray/

Problem Description

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

Solution

Below will explain 4 different approaches to solve this problem.

Solution #1 - Brute Force

Usually start from brute force when you don't have any idea, then step by step to optimize your solution.

Brute Force:(TLE)

Subarray sum, we then need to know subarray range [l, r], 2 for loop, list all possible subarrays, then 1 for loop to calculate current subarray sum, using a global variable to keep track maxSum. This approach has very bad performance,time complexity is O(n^3).

Complexity Analysis

  • Time Complexity: O(n^3) - n array length

  • Space Complexity: O(1)

Solution #2 - PrefixSum + Brute Force

Optimal brute force: (AC)

With brute force approach, we can precalculate prefixSum, so that no need to calculate subarray sum every time, time complexity can reduce to O(n^2)

calculate prefixSum, for giving subarray range [l,r], current subarray sum: subarraySum = prefixSum[r] - prefixSum[l - 1]; global variable maxSum, compare every possible subarray sum to record max sum, maxSum = max(maxSum, subarraySum).

Complexity Analysis

  • Time Complexity: O(n^2) - n array length

  • Space Complexity: O(n) - prefixSum array length n

If update original input array to represent prefix sum, then space will reduce to O(1)

With this optimization, the time complexity is still too high, can we come up better optimization approach.

yes, optimize prefix sum

Solution #3 - optimized prefix sum - from @lucifer

we defineS(i) ,use to calculate sum from range [0, i]

then S(j) - S(i - 1) is sum of range [i, j].

Here, we can get all S[i] , (i = 0,1,2....,n-1) with one loop array. at the same time, we get min sum from S[k], (k = 0,1,i-1), then

maxSum = max(maxSum, S[i] - minSum).

Here we maintain two variables minSum, maxSum.

Complexity Analysis

  • Time Complexity: O(n) - n array length

  • Space Complexity: O(1)

Solution #4 - Divide and Conquer

We partition array nums into two smaller arrays (left and right) from middle index m,

Then we have two arrays:

  • left = nums[0]...nums[m - 1]

  • right = nums[m + 1]...nums[n-1]

The maximum subarray sum can be either one of below three maximum sum: 1. Consider middle element nums[m], Cross left and right subarray, the maximum sum is sum of

maximum left array suffix sum - leftMaxSum, maximum right array prefix sum - rightMaxSum and middle element - nums[m] -> crossMaxSum = leftMaxSum + rightMaxSum + nums[m]

  1. Dont' contain middle element nums[m], maxSum is in left, left do recursive return max.

  2. Don't contain middle element nums[m], maxSum is in right, right do recursive return max.

The maximum sum is max(left, right, crossMaxSum)

For example, nums=[-2,1,-3,4,-1,2,1,-5,4]

maximum subarray sum divide conquer

Complexity Analysis

  • Time Complexity: O(nlogn) - n input array length

  • Space Complexity: O(1)

Solution #5 - Dynamic Programing

Key points of DP is to find DP formula and initial state. Assume we have

dp[i] - maximum sum of subarray that ends at index i

DP formula: dp[i] = max(dp[i - 1] + nums[i], nums[i])

Initial state:dp[0] = nums[0]

From above DP formula, notice only need to access its previous element at each step. In this case, we can use two variables,

currMaxSum - maximum sum of subarray that must end with current index i.

maxSum - global maximum subarray sum

  • currMaxSum = max(currMaxSum + nums[i], nums[i]

  • maxSum = max(currMaxSum, maxSum)

As below pic: maximum subarray sum dp

Complexity Analysis

  • Time Complexity: O(n) - n array length

  • Space Complexity: O(1)

Key Points

  1. Brute force, list all possible subarray, calculate sum for each subarray (use prefixSum to optimize), return max.

  2. Divide and Conquer, from middle index, divide array into left and right part.

    Recursively get left maximum sum and right maximum sum, and include middle element maximum sum.

    return max(leftMaxSum, rightMaxSum, and crossMaxSum).

  3. Dynamic Programming, find DP formula and initial state, and identify initial value, return maximum sum subarray。

Code (Java/Python3)

Solution #2 - PrefixSum + Brute Force

Java code

Python3 code (TLE)

Javascript code from @lucifer

Solution #3

Java code

Python3 code

Javascript code from @lucifer

Solution #4 - Divide and Conquer

Java code

Python3 code

Javascript code from @lucifer

Solution #5 - Dynamic Programming

Java code

Python3 code

Javascript code from @lucifer

Follow Up

  • When given M*N matrix, how to calculate maximum submatrix sum?

  • When given array, return maximum product subarray? compare with maximum sum subarray, what is the difference?

Similar Questions

Last updated

Was this helpful?