Greedy Florist

Sort by

recency

|

791 Discussions

|

  • + 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;
    }
    
  • + 0 comments

    Solution in JAVA

    // Complete the getMinimumCost function below. static int getMinimumCost(int k, int[] c) {

        Arrays.sort(c);
        int res=0;
        int m=0;int counter=0;
        int i=c.length-1;
        for(;i>=0;i--)
        {
    
            if(counter%k==0 && counter>0){
                counter=0;
                m++;
            }
            System.out.println(i+" "+m+" "+counter);
            res=res+(m+1)*c[i];
            counter++;
            System.out.println(res);
        }
        return res;
    
    
    }
    
  • + 0 comments

    Simple Java solution

     static int getMinimumCost(int k, int[] c) {
            Arrays.sort(c);
            int price = 0;
            int count = 0;
            for(int i=c.length-1 ;i>=0; i--){
                price += (count/k+1)*c[i];
                count++;
            }
            return price;
        }