Skip to main content

Supermarket Savings

easy
Software engineer

You are preparing for a camping trip and need to purchase groceries from a supermarket. Each product in the store belongs to a specific department (e.g., "Dairy", "Produce", "Pantry").

You are given:

  1. A 2D list products, where each element is a pair [productName, department] representing a product and its corresponding department.
  2. A list shoppingList, which contains the names of products you plan to buy.

Normally, you would buy products following the order in shoppingList. This might require revisiting the same department multiple times. To save time, you decide to group your purchases by department: you collect all products from one department before moving to the next, minimizing department switches.

Return the number of department visits you save by using this grouped strategy compared to shopping strictly in the order of the shoppingList.

Note:

  • A "visit" is counted every time you enter a department. Moving between two products in the same department consecutively counts as only one visit.
  • The grouped strategy involves visiting each unique department represented in your shoppingList exactly once.

Example 1

Input

products = [["Cheese", "Dairy"], ["Carrots", "Produce"], ["Potatoes", "Produce"], ["Canned Tuna", "Pantry"], ["Romaine Lettuce", "Produce"], ["Chocolate Milk", "Dairy"], ["Flour", "Pantry"], ["Iceberg Lettuce", "Produce"], ["Coffee", "Pantry"], ["Pasta", "Pantry"], ["Milk", "Dairy"], ["Blueberries", "Produce"], ["Pasta Sauce", "Pantry"]]
shoppingList = ["Blueberries", "Milk", "Coffee", "Flour", "Cheese", "Carrots"]

Output

2

Explanation

**1. Identify Departments:**
- Blueberries: Produce
- Milk: Dairy
- Coffee: Pantry
- Flour: Pantry
- Cheese: Dairy
- Carrots: Produce

**2. Original Sequence:**
Produce → Dairy → Pantry → Pantry → Dairy → Produce.
Consecutive duplicates (Pantry → Pantry) count as one visit. The sequence of visits is: Produce (1) → Dairy (2) → Pantry (3) → Dairy (4) → Produce (5). Total = 5.

**3. Grouped Strategy:**
Since there are 3 unique departments (Produce, Dairy, Pantry), you visit each exactly once. Total = 3.

**4. Calculation:**
5 - 3 = 2.

Example 2

Input

products = [["Apple", "Produce"], ["Bread", "Bakery"]], shoppingList = ["Apple", "Bread"]

Output

0

Explanation

Original visits: 2 (Produce → Bakery). Grouped visits: 2. Savings: 0.

Example 3

Input

products = [["A", "X"], ["B", "Y"], ["C", "X"]], shoppingList = ["A", "B", "C", "A"]

Output

2

Explanation

Original departments: X → Y → X → X. Consecutive X's merge. Visits: X, Y, X (3 visits). Unique departments: X, Y (2 visits). Savings: 3 - 2 = 1. Wait, let's re-evaluate: [A(X), B(Y), C(X), A(X)]. Original: X, Y, X (3 visits). Unique: X, Y (2 visits). 3 - 2 = 1.

Constraints

  • 1 <= products.length <= 10⁴
  • 1 <= shoppingList.length <= 10⁴
  • All product names in shoppingList exist in products.

Screening

ArrayCountingHash TableSimulation
Language
Code editor loads in the browser.

Output

Input

[["Cheese","Dairy"],["Carrots","Produce"],["Potatoes","Produce"],["Canned Tuna","Pantry"],["Romaine Lettuce","Produce"],["Chocolate Milk","Dairy"],["Flour","Pantry"],["Iceberg Lettuce","Produce"],["Coffee","Pantry"],["Pasta","Pantry"],["Milk","Dairy"],["Blueberries","Produce"],["Pasta Sauce","Pantry"]]
["Blueberries","Milk","Coffee","Flour","Cheese","Carrots"]

Expected

2

Your output

Run to see your output.