Greedy Florist Solution in javascript


Greedy Florist
Hackerrank solution in javascript

Problem
You and friends want to buy flowers. Each flower has some cost . The florist is greedy and wants to maximize his number of new customers, so he increases the sale price of flowers for repeat customers; more precisely, if a customer has already purchased flowers, price for is .
Find and print the minimum cost for your group to purchase flowers.
Note: You can purchase the flowers in any order.
Input Format
The first line contains two integers, (number of flowers to purchase) and (the size of your group of friends, including you). 
The second line contains space-separated positive integers describing the cost () for each flower .



 function processData(input) {  
   input = input.split('\n');  
   let n = parseInt(input[0].split(' ')[0]);  
   let k = parseInt(input[0].split(' ')[1]);  
   let flowers = input[1].split(' ').map(Number).sort((a,b)=>{return b-a});  
   let price = 0;  
   let x =0;  
   for (let i=0; i<n;i++){  
     if(i%k===0 && i!==0){  
       x++;  
     }  
     price +=((x+1)*flowers[i]);  
   }  
   //console.log('Final Price '+price);  
     
   console.log(price);  
     
 }   
   
 process.stdin.resume();  
 process.stdin.setEncoding("ascii");  
 _input = "";  
 process.stdin.on("data", function (input) {  
   _input += input;  
 });  
   
 process.stdin.on("end", function () {  
   processData(_input);  
 });  
   
   

Σχόλια

Δημοφιλείς αναρτήσεις