FastPrepMinimum Stores for a Shopping List

Minimum Stores for a Shopping List

WhatNot logoWhatNot● HardFULLTIMENEW GRADPHONE SCREEN
Learn

Problem statement

You have a shopping list and a collection of stores. Each store carries a set of items. Return the minimum number of distinct stores you must visit so that every item on the shopping list can be purchased.

An item may be purchased from any visited store that carries it. Duplicate names in the shopping list represent the same required item. Return -1 when at least one required item cannot be covered.

Function

minimumStores(stores: String[][], shoppingList: String[]) → int

Examples

Example 1

stores = [["milk","bread"],["eggs"],["bread","eggs"]]shoppingList = ["milk","eggs"]return = 2

No single store carries both required items, so two visits are necessary.

Example 2

stores = [["apple","tea"],["tea","rice","apple"],["rice"]]shoppingList = ["apple","rice","tea"]return = 1

The second store covers the entire list.

Example 3

stores = [["a"],["b"]]shoppingList = ["a","c"]return = -1

No store carries c.

Constraints

  • 0 <= stores.length <= 50.
  • 0 <= shoppingList.length <= 20.
  • Every item name is a non-empty case-sensitive ASCII string of length at most 40.
  • Repeated item names within a store do not change its coverage.

More WhatNot problems

See WhatNot hiring insights
public int minimumStores(String[][] stores, String[] shoppingList) {
    // Write your solution here.
}
stores[["milk","bread"],["eggs"],["bread","eggs"]]
shoppingList["milk","eggs"]
expected2
Checking account…