You are given data about the prices of products at different stores. For each product, you want to find out its price at every store. The data is provided as a list of records, where each record contains a product
name, a store
name, and the price
of that product at that store.
Your task is to generate a mapping for each store that shows the price of every product available at that store. If a product is not sold at a particular store, it should be omitted from that store's mapping.
The key constraints are:
The first step is to consider how to organize the data so that it is easy to look up the price of any product at any store. A brute-force approach would be to iterate through the list for every store and every product, but this would be inefficient and repetitive.
A more efficient approach is to group the data by store first, and within each store, map each product to its price. This way, we avoid unnecessary checks and make lookups fast and direct. Using a dictionary (hash map) for both stores and products is ideal because it allows for quick insertion and retrieval.
The main idea is to build a nested dictionary: the outer dictionary's keys are store names, and each value is another dictionary mapping product names to their prices at that store.
Let's break down the algorithm step by step:
store
, product
, and price
.
We use dictionaries because they provide O(1) time complexity for insertion and lookup, making the process efficient even for large datasets.
Suppose the input is:
Let's process each record:
{ "StoreA": {"apple": 1, "banana": 2}, "StoreB": {"apple": 3, "orange": 4} }
Brute-force approach:
The optimized approach is much faster because it processes each record only once and uses hash maps for constant-time access.
In summary, the problem is elegantly solved by using a nested dictionary structure: the outer dictionary maps stores to their products, and the inner dictionary maps products to their prices. This approach is both efficient and easy to implement, avoiding unnecessary computations and making lookups fast. The key insight is to group data by store first, then by product, leveraging the power of hash maps for organization and efficiency.
def products_price_for_each_store(records):
result = {}
for record in records:
store = record['store']
product = record['product']
price = record['price']
if store not in result:
result[store] = {}
result[store][product] = price
return result
#include <unordered_map>
#include <string>
#include <vector>
using namespace std;
struct Record {
string product;
string store;
int price;
};
unordered_map<string, unordered_map<string, int>> productsPriceForEachStore(const vector<Record>& records) {
unordered_map<string, unordered_map<string, int>> result;
for (const auto& record : records) {
result[record.store][record.product] = record.price;
}
return result;
}
import java.util.*;
class Record {
String product;
String store;
int price;
Record(String product, String store, int price) {
this.product = product;
this.store = store;
this.price = price;
}
}
public class Solution {
public Map<String, Map<String, Integer>> productsPriceForEachStore(List<Record> records) {
Map<String, Map<String, Integer>> result = new HashMap<>();
for (Record record : records) {
result.computeIfAbsent(record.store, k -> new HashMap<>()).put(record.product, record.price);
}
return result;
}
}
function productsPriceForEachStore(records) {
const result = {};
for (const record of records) {
const { store, product, price } = record;
if (!(store in result)) {
result[store] = {};
}
result[store][product] = price;
}
return result;
}