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 Java 8 solution, passing all tests, for Max Score of 35:
classResult{privatestaticclassIceCreamIdCostBinding{privateintid;privateintcost;// ConstructorpublicIceCreamIdCostBinding(intid,intcost){this.id=id;this.cost=cost;}// Getter for idpublicintgetId(){returnid;}// Getter for costpublicintgetCost(){returncost;}publicStringtoString(){return"{ id = >"+id+"< | cost = >"+cost+"< }";}}/* * Complete the 'whatFlavors' function below. * * The function accepts following parameters: * 1. INTEGER_ARRAY cost * 2. INTEGER money */publicstaticvoidwhatFlavors(List<Integer>cost,intmoney){// Write your code hereList<IceCreamIdCostBinding>iceCreamIdCostBindingsLst=newArrayList<>();for(intcIdx=0;cIdx<cost.size();cIdx++){iceCreamIdCostBindingsLst.add(newIceCreamIdCostBinding((cIdx+1),cost.get(cIdx)));}iceCreamIdCostBindingsLst.sort(Comparator.comparingInt(IceCreamIdCostBinding::getCost));intsortedArrEndIdx=Collections.binarySearch(iceCreamIdCostBindingsLst,newIceCreamIdCostBinding(0,money),Comparator.comparingInt(IceCreamIdCostBinding::getCost));// If value not found, binarySearch returns (-(insertion point) - 1)if(sortedArrEndIdx<0){sortedArrEndIdx=(iceCreamIdCostBindingsLst.size()-1);}else{sortedArrEndIdx--;}for(intcIdx=0;cIdx<sortedArrEndIdx;cIdx++){intremainingMoney=(money-iceCreamIdCostBindingsLst.get(cIdx).getCost());List<IceCreamIdCostBinding>searchSubLst=iceCreamIdCostBindingsLst.subList((cIdx+1),(sortedArrEndIdx+1));intremMoneySubLstIdx=Collections.binarySearch(searchSubLst,newIceCreamIdCostBinding(0,remainingMoney),Comparator.comparingInt(IceCreamIdCostBinding::getCost));if(remMoneySubLstIdx>=0){inticeCreamID1=iceCreamIdCostBindingsLst.get(cIdx).getId();inticeCreamID2=searchSubLst.get(remMoneySubLstIdx).getId();System.out.println(Math.min(iceCreamID1,iceCreamID2)+" "+Math.max(iceCreamID1,iceCreamID2));break;}}}}
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 Java 8 solution, passing all tests, for Max Score of 35: