Advertisement
kstoyanov

05. Lowest Prices in Cities

Sep 20th, 2020
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.   const map = new Map();
  3.  
  4.   arr.forEach((line) => {
  5.     const [town, product, price] = line.split(' | ');
  6.  
  7.     if (!map.has(product)) {
  8.       map.set(product, new Map());
  9.     }
  10.  
  11.     map.get(product).set(town, Number(price));
  12.   });
  13.  
  14.   for (const [product, insideMap] of map) {
  15.     let lowestPrice = Number.POSITIVE_INFINITY;
  16.     let townWithLowestPrice = '';
  17.     for (const [town, price] of insideMap) {
  18.       if (price < lowestPrice) {
  19.         lowestPrice = price;
  20.         townWithLowestPrice = town;
  21.       }
  22.     }
  23.  
  24.     console.log(`${product} -> ${lowestPrice} (${townWithLowestPrice})`);
  25.   }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement