Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- const map = new Map();
- arr.forEach((line) => {
- const [town, product, price] = line.split(' | ');
- if (!map.has(product)) {
- map.set(product, new Map());
- }
- map.get(product).set(town, Number(price));
- });
- for (const [product, insideMap] of map) {
- let lowestPrice = Number.POSITIVE_INFINITY;
- let townWithLowestPrice = '';
- for (const [town, price] of insideMap) {
- if (price < lowestPrice) {
- lowestPrice = price;
- townWithLowestPrice = town;
- }
- }
- console.log(`${product} -> ${lowestPrice} (${townWithLowestPrice})`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement