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.
Beautiful. In my solution I looped over the hash table twice, so a little bit more lines of code:
functionwhatFlavors(cost,money){constcache={};// fill cache with {[cost] : [index, ...]} pairs// in case if there are repeating valuesfor(leti=0;i<cost.length;i++){constvalue=cache[cost[i]]||[];value.push(i);cache[cost[i]]=value;}// console.log(cache);constresult=[];// loop over the costs stored in keys of the cachefor(constkeyincache){// calc missing valueconstdiff=money-(+key);if(diffincache){leti=0;letj=0;// if it's a repeating value// it's stored in the seconds positionif(diff==key)j=1;result.push(cache[key][i]+1);result.push(cache[diff][j]+1);break;}}const[min,max]=[Math.min(...result),Math.max(...result)];returnconsole.log(min,max);}
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 →
Beautiful. In my solution I looped over the hash table twice, so a little bit more lines of code: