Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- const countries = new Map();
- for(let line of arr) {
- let [country, city, price] = line.split(' > ');
- if(!countries.has(country)) {
- let obj = {
- [city]: Number(price),
- }
- countries.set(country, obj);
- } else {
- if(countries.get(country)[city] != undefined) {
- let oldPrice = countries.get(country)[city];
- if(price < oldPrice) {
- countries.get(country)[city] = price;
- }
- } else {
- countries.get(country)[city] = price;
- }
- }
- }
- let sortedEntries = Array.from(countries.entries()).sort();
- sortedEntries.forEach(entrie => {
- let [country, obj] = entrie;
- let objectEntries = Object.entries(obj);
- if(objectEntries.length > 1) {
- objectEntries = objectEntries.sort((a, b) => {
- costA = a[1];
- costB = b[1];
- return costA - costB;
- });
- let resultString = '';
- objectEntries.forEach(city => {
- let str = (city.join(' -> '));
- resultString += str + ' ';
- });
- console.log(`${country} -> ${resultString}`);
- } else {
- let str = objectEntries[0].join(' -> ');
- console.log(`${country} -> ${str}`);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement