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.
publicintgetMinimumCost(intk,int[]c){Array.Sort(c);IDictionary<int,int>dict=newDictionary<int,int>();intsum=0;// first sell most expensive flower to each personfor(inti=c.Length-1;i>=c.Length-k;i--)sum+=c[i];// current personintcurrent=0;for(inti=c.Length-k;i>0;i--){// counter how many flowers every person// alredy boughtif(dict.ContainsKey(current))dict[current]+=1;elsedict.Add(current,1);// calculate sum with current price and count of// flower current person already boughtsum+=(dict[current]+1)*c[i-1];// move to next personcurrent++;// if all persons bought same number of flowers// reset current person to firstif(current==k)current=0;}returnsum;}
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 →
C# solution with Dictionary, Pass 100%.