We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
here's my javascript solution as well. not quite as pretty - I built the map first, then went over the costs. is there any advantage to creating the map first, then going over the costs...? guessing my solution is O(2n) vs O(n)
functionwhatFlavors(cost,money){letcostTables={}// create hashmap of cost -> array of flavorsfor(letflavor=0;flavor<cost.length;flavor++){letflavorCost=cost[flavor]if(flavorCostincostTables){// if we've already got an entry for our flavor cost// then add this flavor ID to itcostTables[flavorCost].push(flavor+1)}else{costTables[flavorCost]=[flavor+1]}}for(letsunnyDollarsofcost){letjohnnyDollars=money-sunnyDollarsif(johnnyDollarsincostTables&&sunnyDollarsincostTables){// if they pooled the same amount of money, we're looking at// the same cost table. We have to check if the cost table// entry has at least 2 ids for two flavors of ice creamif(johnnyDollars===sunnyDollars){if(costTables[sunnyDollars].length===2){letpurchase=sortIds(costTables[sunnyDollars])console.log(`${purchase[0]}${purchase[1]}`)return}}else{letpurchase=sortIds(costTables[sunnyDollars].concat(costTables[johnnyDollars]))console.log(`${purchase[0]}${purchase[1]}`)return}}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Hash Tables: Ice Cream Parlor
You are viewing a single comment's thread. Return to all comments →
here's my javascript solution as well. not quite as pretty - I built the map first, then went over the costs. is there any advantage to creating the map first, then going over the costs...? guessing my solution is O(2n) vs O(n)