Iroh’s Tea Brewing - codepath/compsci_guides GitHub Wiki

Unit 12 Session 1 Advanced (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 25-30 mins
  • 🛠️ Topics: Dynamic Programming, Arrays

1: U-nderstand

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?

    • The goal is to find the subarray of consecutive ingredients that produces the largest product of strengths.
  • How do negative values affect the problem?

    • Negative values can reverse the effect of multiplication, which means the minimum product so far might turn into the maximum product if multiplied by a negative number.
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.

2: M-atch

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:

  • Dynamic Programming (DP): We need to track the maximum and minimum product at each step because a negative value might flip the maximum into a minimum and vice versa.
  • Greedy/Sliding Window: This doesn't work well here because of the importance of negative values.

3: P-lan

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.

Steps:

  1. Initialization:

    • Create two arrays dp_max and dp_min to store the maximum and minimum products at each index.
    • Initialize both arrays with the value of the first ingredient.
    • Set result to the first element of the array.
  2. Iterate through the ingredients:

    • For each ingredient:
      • If the current ingredient is positive, update dp_max[i] to the maximum of the current ingredient or the product of the current ingredient and dp_max[i-1].
      • Similarly, update dp_min[i] for negative values.
      • If the current ingredient is negative, swap the maximum and minimum values before updating to account for sign reversal.
  3. Update Result:

    • For each iteration, update the result with the largest value of dp_max[i].
  4. Return the Result:

    • Return the final value of result, which will contain the largest product of any subarray.

4: I-mplement

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

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

Example 1:

  • Input: [1, 2, -3, 4]
  • Expected Output: 4

Example 2:

  • Input: [-2, -1]
  • Expected Output: 2

6: E-valuate

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.

  • Time Complexity: O(n) because we iterate through the array once.
  • Space Complexity: O(n) because we use two additional arrays (dp_max and dp_min) to store the results at each index.