Greedy Florist

Sort by

recency

|

787 Discussions

|

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

    I find the most optimal solution of this question :) Just sort the array desc to asc and divide the index with k where u r geeting change in formulae

    def getMinimumCost(k, c):
        c.sort(reverse=True)
        sum=0
        for i in range(len(c)):
            sum+=(int(i/k)+1)*c[i]
        return sum
    
  • + 0 comments
    1. Sort array ascending which is default.
    2. Make multiple groups of k elements each, from right to left.
    3. Iterate left to right within each group.
    4. Increase previous purchase by one after iterating each group.