Unit 12 Session 1 Advanced (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
What is the goal of the problem?
How do negative values affect the problem?
HAPPY CASE
Input:
ingredients = [1, 2, -3, 4]
Output:
4
Explanation:
The strongest tea is brewed with the last ingredient only, which has a strength of 4.
EDGE CASE
Input:
ingredients = [-2, -1]
Output:
2
Explanation:
The strongest tea is brewed using both ingredients. The product of -2 and -1 is 2.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For maximum product subarray problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use dynamic programming to maintain two arrays: one for the maximum product and one for the minimum product up to each index. The maximum result at each index will be the largest value of the current element or the product of the current element with the previous max/min products.
Initialization:
dp_max and dp_min to store the maximum and minimum products at each index.result to the first element of the array.Iterate through the ingredients:
dp_max[i] to the maximum of the current ingredient or the product of the current ingredient and dp_max[i-1].dp_min[i] for negative values.Update Result:
dp_max[i].Return the Result:
result, which will contain the largest product of any subarray.Implement the code to solve the algorithm.
def strongest_tea(ingredients):
if not ingredients:
return 0
n = len(ingredients)
# Initialize dp arrays
dp_max = [0] * n
dp_min = [0] * n
# Base case
dp_max[0] = ingredients[0]
dp_min[0] = ingredients[0]
# Initialize result with the first element
result = ingredients[0]
# Fill the dp arrays
for i in range(1, n):
if ingredients[i] >= 0:
dp_max[i] = max(ingredients[i], dp_max[i - 1] * ingredients[i])
dp_min[i] = min(ingredients[i], dp_min[i - 1] * ingredients[i])
else:
dp_max[i] = max(ingredients[i], dp_min[i - 1] * ingredients[i])
dp_min[i] = min(ingredients[i], dp_max[i - 1] * ingredients[i])
# Update the result
result = max(result, dp_max[i])
return result
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
[1, 2, -3, 4]
4
Example 2:
[-2, -1]
2
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the size of the input array ingredients.
O(n) because we iterate through the array once.O(n) because we use two additional arrays (dp_max and dp_min) to store the results at each index.