Greedy Florist


Problem Statement :


A group of friends want to buy a bouquet of flowers. The florist wants to maximize his number of new customers and the money he makes. To do this, he decides he'll multiply the price of each flower by the number of that customer's previously purchased flowers plus 1. The first flower will be original price, ( 0 + 1 ) * original price , the next will be  ( 1 + 1 ) * original price and so on.

Given the size of the group of friends, the number of flowers they want to purchase and the original prices of the flowers, determine the minimum cost to purchase all of the flowers.


Function Description

Complete the getMinimumCost function in the editor below. It should return the minimum cost to purchase all of the flowers.

getMinimumCost has the following parameter(s):

c: an array of integers representing the original price of each flower
k: an integer, the number of friends

Input Format

The first line contains two space-separated integers n and k, the number of flowers and the number of friends.
The second line contains n space-separated positive integers c[ i ] , the original price of each flower.

Constraints

1  <=  n. k  <= 100
1  <=  c[ i ]  <=  10^6
answer  <   2^31
0   <=  i  <  n

Output Format

Print the minimum cost to buy all n flowers.



Solution :



title-img


                            Solution in C :

In  C :






#include <stdio.h>
#include <stdlib.h>

long long a[200],i,j,k,l,m,n;

int com(const void *xx, const void *yy)
{
if(*(long long *)xx > *(long long*)yy) return -1;
return 1;
}

int main()
{

scanf("%lld %lld\n",&n,&k);
for(i=0;i<n;i++) scanf("%lld",a+i);

qsort(a,n,sizeof(a[0]),com);

m = 0;

for(i=0;i<n;i++) m+= a[i]*(1+i/k);

printf("%lld\n",m);


return 0;
}
                        


                        Solution in C++ :

In  C++ :




#include <cstdio>
#include <algorithm>
using namespace std;

int a[102];

int main() {
int i,j,m,n;
long long c,ans=0;
	scanf("%d%d",&n,&m);
	for (i=0;i<n;++i) {
		scanf("%d",&a[i]);
	}
	sort(a,a+n);
	for (i=n-1,c=0,j=0;i>=0;--i) {
		if (j==0) {
			++c;
		}
		ans+=c*a[i];
		if (++j==m) {
			j=0;
		}
	}
	printf("%Ld\n",ans);
	return 0;
}
                    


                        Solution in Java :

In  Java :






import java.util.Arrays;
import java.util.Scanner;

public class Solution {

    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);

        int n, k;
        n = in.nextInt();
        k = in.nextInt();

        int c[] = new int[n];
        for (int i = 0; i < n; i++) {
            c[i] = in.nextInt();
        }

        Arrays.sort(c);
        
        int result = 0;
        
        if(k >= n){
            for(int i=0;i<n;i++){
                result +=  c[i];
            }
            System.out.println(result);
        }else{
            //Processing
            int x = 0;            
            while(n > 0){                
                for(int i=0;i<k;i++){
                    result += c[n-1]*(x+1);
                    n--;
                    if(n == 0){
                        break;
                    }
                }                
                x++;
            }
            System.out.println(result);
        }        
    }
}
                    


                        Solution in Python : 
                            
In Python3 :




NK = input()

N = int(NK.partition(' ')[0])
K = int(NK.partition(' ')[2])

prices = [int(x) for x in input().strip().split(' ')]

if K == N:
    print(sum(prices))
else:
    prices.sort()
    friends = [[] for friend in range(K)]
    
    for friend in friends:
        friend.append(prices.pop())
    
    index = 0
    while prices:
        friends[index].append(prices.pop()*(1+len(friends[index])))
        index = (index + 1)%K
        
    total = 0
    for friend in friends:
        total += sum(friend)
        
    print(total)
                    


View More Similar Problems

Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →

Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →