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.
// k: number of friends// c[i]: [price of each flower]functiongetMinimumCost(k,c){if(k>=c.length)returnc.reduce((acc,cur)=>acc+cur,0)else{c.sort((a,b)=>b-a)letboughtCountEach=newArray(k).fill(0),boughtCount=0letcosts=0while(boughtCount<c.length){letwhoBuyThisTime=boughtCountEach.findIndex(val=>val==Math.min(...boughtCountEach))costs+=(boughtCountEach[whoBuyThisTime]+1)*c[boughtCount]boughtCountEach[whoBuyThisTime]+=1boughtCount++}returncosts}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Greedy Florist
You are viewing a single comment's thread. Return to all comments →
javascript