Greedy florist hackerrank solution in python

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Greedy Florist in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To Algorithm

The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results. 

Advantages of Algorithms:

  • It is easy to understand.
  • Algorithm is a step-wise representation of a solution to a given problem.
  • In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.

Link for the Problem – Greedy Florist – Hacker Rank Solution

Greedy Florist– Hacker Rank Solution

Problem:

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,

Function Description

Complete the getMinimumCost function in the editor below.

getMinimumCost has the following parameter[s]:

  • int c[n]: the original price of each flower
  • int k: the number of friends

Returns
– int: the minimum cost to purchase all flowers

Input Format

The first line contains two space-separated integers  and , the number of flowers and the number of friends.
The second line contains  space-separated positive integers , the original price of each flower.

Constraints

Sample Input 0

3 3 2 5 6

Sample Output 0

13

Explanation 0

Sample Input 1

3 2 2 5 6

Sample Output 1

15

Explanation 1

Greedy Florist – Hacker Rank Solution import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { // Complete the getMinimumCost function below. static int getMinimumCost[int k, int[] c] { Arrays.sort[c]; int ans=0; int count=1; int noOfFriendsRem=k; for [int i = c.length-1; i >-1; i--] { if [noOfFriendsRem==0] { noOfFriendsRem=k; count++; } ans+=c[i]*count; noOfFriendsRem--; } return ans; } 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[]; } }

You can’t perform that action at this time.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.

Page 2

You can’t perform that action at this time.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.

Greedy florist wants to sell flowers for more money, and created the following rules:

For a group of k customers to buy n flowers with associated cost [c[0], c[1], …c [n-1]]. First time buyer pay original price, second time buyer will have to pay twice the price, and third time buyer will have to pay third the price…

What’s the algorithm to calculate the minimum cost for the k poor customers?

Link: //www.hackerrank.com/challenges/greedy-florist/problem

Thinking process:

The strategy is to use the first few times to buy the most expensive flowers, and buy the least expensive flowers the last. To do this, we will need to sort the cost to descending order, and start buying flowers from the start of the list. Let’s look at the code:

Python 3 Solution:

time: 0[nLogn] because we used quick sort

memory: o[1]

Comments and suggestions welcome!

In this HackerRank Greedy Florist interview preparation kit problem, you need to Complete the getMinimumCost function in the editor.

Problem solution in Python programming.

N, k = [int[i] for i in input[].split[' ']] ci = [int[i] for i in input[].split[' ']] ci.sort[] if N-1; i--] { if [noOfFriendsRem==0] { noOfFriendsRem=k; count++; } ans+=c[i]*count; noOfFriendsRem--; } return ans; } 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[]; } }

Problem solution in C++ programming.

#include #include 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=0;--i] { if [j==0] { ++c; } ans+=c*a[i]; if [++j==m] { j=0; } } printf["%Ld\n",ans]; return 0; } #include #include #include #define MAX 101 int cmp[const void *a, const void *b] { return *[int *]b - *[int *]a; } int getPrice[int arr, int cus[], int ix] { return [cus[ix]+1]*arr; } int main[void] { int i, ix; int N, K, sum; int arr[MAX], cus[MAX]; memset[arr, 0, sizeof[arr]]; memset[cus, 0, sizeof[cus]]; scanf["%d %d", &N, &K]; for[i = 0; i < N; ++i] scanf["%d", &arr[i]]; qsort[arr, N, sizeof[int], cmp]; sum = 0; ix = 0; for[i = 0; i < N; ++i]{ sum += getPrice[arr[i], cus, ix%K]; ++cus[ix%K]; ix = [++ix]%K; } printf["%d\n", sum]; return 0; }

Problem solution in JavaScript programming.

process.stdin.resume[]; process.stdin.setEncoding["ascii"]; process.stdin.on["data", function [input] { function sortNumber[a,b] { return b - a; } var a = input.split["\n"]; b = a[0]; c = a[1]; var d = b.split[" "]; var n = d[0]; var k = d[1]; var prices = c.split[" "]; var op = prices.sort[sortNumber]; var total=0; for [var i=0; i

Chủ Đề