Greedy Florist

Sort by

recency

|

789 Discussions

|

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

    Java Solution using Priority Queue -> public class Solution {

    // Complete the getMinimumCost function below.
    static int getMinimumCost(int k, int[] c) {
    
        Arrays.sort(c);
    
        PriorityQueue<Pair> q = new PriorityQueue<>((a,b)->
        {
            return a.cnt-b.cnt;
        });
        int s = 0;
        if(c.length<k)
        {
            for(int i =0;i<c.length;i++)
            {
              s=s+c[i];  
            }
            return s;
        }
        int l = c.length-1;
        for(int i =0;i<k;i++)
        {
            q.offer(new Pair(1,i));
            s = s+c[l];
            l--;       
        }
    
        while(l>=0)
        {
            Pair temp = q.poll();
            System.out.println("cnt here is"+temp.cnt);
            int v = (temp.cnt+1)*c[l];
            s=s+v;
            q.offer(new Pair(temp.cnt+1,temp.val));
            l--;
    
    
        }
    
        return s;
    
    }
    
    private static final Scanner scanner = new Scanner(System.in);
    
    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
    
        String[] nk = scanner.nextLine().split(" ");
    
        int n = Integer.parseInt(nk[0]);
    
        int k = Integer.parseInt(nk[1]);
    
        int[] c = new int[n];
    
        String[] cItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    
        for (int i = 0; i < n; i++) {
            int cItem = Integer.parseInt(cItems[i]);
            c[i] = cItem;
        }
    
        int minimumCost = getMinimumCost(k, c);
    
        bufferedWriter.write(String.valueOf(minimumCost));
        bufferedWriter.newLine();
    
        bufferedWriter.close();
    
        scanner.close();
    }
    

    }

  • + 0 comments

    Python 3 - sort desc, caclulate multiplier by flooring the division of flower index and number of friends (this is the "round" of purchase), multiply times price, loop exists at last flower.

    def getMinimumCost(k, c):
        c = sorted(c, reverse=True) 
    
        total_cost = 0
    
        for i in range(len(c)):
            multiplier = (i // k) + 1  
            total_cost += multiplier * c[i]
    
        return total_cost