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.
My Java 8 solution, which passes all test cases, for max score of 35:
Code is fairly simple & self-explanatory.
Sort the Array + simple greedy approach after that.
// Complete the getMinimumCost function below.staticintgetMinimumCost(intk,int[]c){intminFlowerCost=0;intnumFriends=k;intnumFlowers=c.length;Arrays.sort(c);List<Integer>friendNumFlowers=IntStream.range(0,numFriends).mapToObj(i->0).collect(Collectors.toCollection(ArrayList::new));intfriendIdx=0;for(intcIdx=(numFlowers-1);cIdx>=0;cIdx--){minFlowerCost+=((friendNumFlowers.get(friendIdx)+1)*c[cIdx]);friendNumFlowers.set(friendIdx,(friendNumFlowers.get(friendIdx)+1));friendIdx=((friendIdx+1)%numFriends);}returnminFlowerCost;}
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 →
My Java 8 solution, which passes all test cases, for max score of 35:
Code is fairly simple & self-explanatory.
Sort the Array + simple greedy approach after that.