Greedy Florist

Sort by

recency

|

793 Discussions

|

  • + 0 comments

    Here is my Python solution!

    def getMinimumCost(k, c):
        flowers = len(c)
        c.sort()
        price = 0
        for multiplier in range(1, flowers // k + 1):
            for num in range(k):
                price += multiplier * c.pop()
        price += sum(c) * (flowers // k + 1)
        return price
    
  • + 0 comments
    def getMinimumCost(k, c):
        c.sort(reverse = True)
        sum = 0
        for idx, price in enumerate(c):
            sum += price * (1 + idx // k)
        return sum
    
  • + 0 comments

    JS

    function getMinimumCost(k, c) {
      let min = 0;
    
      // sort flowers - highest to lowest
      c.sort((a, b) => (a - b < 0 ? 1 : -1));
    
      for (let buyerIdx = 0; buyerIdx < k; buyerIdx++) {  // e.g. 3 friends = 3 buyers
        // with every purchase the next purchased flower gets multiplied
        let multiplier = 1;
    
        // to have the lowest possible multiplier on the hightest prices,
        //  -> buy every "k" flower (flowerIdx += k) in the price-sorted array
        for (let flowerIdx = buyerIdx; flowerIdx < c.length; flowerIdx += k) {
          min += c[flowerIdx] * multiplier++;
        }
      }
    
      return min;
    }
    
  • + 0 comments

    Java Solution

    import java.util.*;
    
    public class Solution {
    
        static int getMinimumCost(int numberOfFriends, Integer[] prices) {
            Arrays.sort(prices, Collections.reverseOrder());
    
            int i = 0;
            int numberOfBoughtFlowers = 0;
            int minimumCost = 0;
            
            while (i < prices.length) {
                for (int j = 0; j < numberOfFriends && i < prices.length; j++) {
                    minimumCost += (1 + numberOfBoughtFlowers) * prices[i];
                    i++;
                }
                
                numberOfBoughtFlowers++;
            }
            
            return minimumCost;
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            final String SPACE = " ";
            String[] amounts = scanner.nextLine().split(SPACE);
            int numberOfFlowers = Integer.parseInt(amounts[0]);
            int numberOfFriends = Integer.parseInt(amounts[1]);
    
            Integer[] prices = new Integer[numberOfFlowers];
            String[] priceItems = scanner.nextLine().split(SPACE);
    
            for (int i = 0; i < numberOfFlowers; i++) {
                int price = Integer.parseInt(priceItems[i]);
                prices[i] = price;
            }
    
            int minimumCost = getMinimumCost(numberOfFriends, prices);
            System.out.println(minimumCost);
            
            scanner.close();
        }
    }
    
  • + 0 comments

    c# solution:

    static int getMinimumCost(int k, int[] c) {
    
         int howManyFlowers = c.Count();
         Array.Sort(c);
        int cost = 0;
        int multipleCost = 1;
        int couterForMultipleCost = 0;
        if (howManyFlowers <= k)
        {
            //cost = c.Sum();
            return c.Sum();
        }
        else
        {
            do
            {
            couterForMultipleCost++;
            if (couterForMultipleCost > k)
            {
                couterForMultipleCost = 1;
                multipleCost++;
            }
            cost += multipleCost * c[howManyFlowers - 1];
            howManyFlowers -= 1;
            } while (howManyFlowers != 0);
        }
        return cost;
    }