Abstract
- Kadane’ algorithm: given an Array of elements , the maximum sum of contiguous subarray at index , is the maximum of element at index , and the sum of element at index and the maximum sum of contiguous subarray at index ,
- From the Recurrence Function , we can see that if the maximum sum of contiguous subarray at index is negative, the maximum sum of contiguous subarray at index , is definitely the element at index , in another word, we are actually starting a new subarray
Attention
Maximum sum of contiguous subarray at index must include the element at index .
Maximum Subarray Problem
- Maximum Subarray Problem is the task of finding the largest possible sum of a contiguous subarray, within a given one-dimensional Array of numbers
- This problem can be solved efficiently in using Kadane’s algorithm which computes the largest possible sum of a contiguous subarray at each index. The answer is just the biggest number among all the largest possible sum of contiguous subarray at each index
Attention
The contiguous subarray is an non-empty subarray. If the given array only has negative numbers, the maximum subarray should be an empty subarray which has a sum of . But the solution with Kadane’s algorithm will return the smallest value from the array.
We can definitely modify the solution a bit to adapt to this edge case. For example, we can have a variable that returns true if all the elements in the array are negative, then we just return when the variable is true.
Top-down DP Approach
- This Top-down DP Approach doesn’t require DP Table as the Optimal Substructure (最优子结构) removes the Overlapping Subproblems (重复子问题). The State Transition Equation (状态转移方程) shown above is the core of this top-down DP approach
- The
globalMax
variable is used to keep track of the biggest number among all the largest possible sum of contiguous subarray at each index
Bottom-up DP Approach
- In Bottom-up DP Approach, we can see the maximum subarray with the inclusion of element at current index is the maximum of the element at the current index and the sum of element at current index + maximum subarray at the previous index
- The
globalMax
variable is used to keep track of the biggest number among all the largest possible sum of contiguous subarray at each index
Maximum Product Subarray
- Maximum Product Subarray is the task of finding the largest possible product of a contiguous subarray, within a given one-dimensional Array of numbers
Key point
It is a more advanced version of Maximum Subarray Problem, because the largest possible product can consist of negative numbers since negative negatives produces positive. However, negative positive produces negative.
If we use Kadane’s algorithm to find the maximum product of a contiguous subarray at index , there is NO WAY to find the largest product if it consists of two negative numbers, for example. This is because, once we encounter a negative number, we discard it since a positive number multiplied by a negative number is negative, unless the two negative numbers are the first elements. The solution is to keep track of the minimum product of a contiguous subarray at index as well, and the maximum product of a contiguous subarray at index depends on it!
Top-down DP Approach
- This Top-down DP Approach requires
maxArr
andminArr
to function as DP Table to avoid duplicated computation - The State Transition Equation (状态转移方程) shown above is the core of this top-down DP approach. The
globalMax
variable is used to keep track of the biggest number among all the largest possible product of contiguous subarray at each index
Bottom-up DP Approach
- In Bottom-up DP Approach, we can see the maximum subarray with the inclusion of element at current index is the maximum of the element at the current index, minimum product at the previous index element at current index, maximum product at the previous index element at current index
- The
globalMax
variable is used to keep track of the biggest number among all the largest possible product of contiguous subarray at each index
Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock is the task of finding the largest possible profit of a given one-dimensional Array of numbers, the profit is the difference between the number at the right side and the number on the left side
Key point
This can be solved with Kadane’s algorithm, we calculate the minimum cost of purchasing the stock at each index, which is used to calculate the profit on the next index. Each index here denotes a specific day.
Top-down DP Approach
- This Top-down DP Approach requires
minArr
to function as DP Table to avoid duplicated computation - The State Transition Equation (状态转移方程) shown above is the core of this top-down DP approach. The
maxProfit
variable is used to keep track of the biggest profit among all the best deals we can make on each day
Bottom-up DP Approach
- In Bottom-up DP Approach, we use
minCost
to hold the smallest cost of purchase a stock til that particular index, which is used to calculate the best deal on the next index - The
maxProfit
variable is used to keep track of the biggest profit among all the best deals we can make on each day