Organizing containers of balls hackerrank solution
It is maybe not so evident from the description of the challenge that the containers can have different capacities. All the examples that were given had same-sized containers. To better grasp the problem, I find it useful to work with an example that has different sized containers: Show So we have 3 containers (named A, B and C), and 3 types of balls (red, blue, green). Note that container B is the smallest: apparently it can contain 5 balls. A can contain 6 balls, and C can contain 7 balls. A key observation is that if we first empty the containers and then freely distribute the balls over the containers, filling them again, there is always a way to achieve that same transition through mere swapping. You may need a moment to let this sink in and to verify this is indeed the case. But once you get this principle, you can just forget about the swapping part, and approach the problem from an angle where you first remove all the balls and then distribute them again. So let's empty the containers. We then have the following balls "in our hands": red | blue | green -----+------+------- 7 | 6 | 5Now ask yourself: if container B has a capacity of 5, which kind of balls would you put in there? Obviously the green balls. It wouldn't work with any other type of ball, since you would then have to put one or more of them in another container, which violates the requirements. The general rule is thus: put the balls of which you have the least in the container that has the smallest capacity. If that number of balls doesn't match the capacity, then there is just no way to get to a solution. If it does match, then you can repeat this with the next kind of balls (in increasing order) and next container (in increasing capacity). And so you see that actually sorting the containers and balls like that is a way to find a solution. Taum is planning to celebrate the birthday of his friend, Diksha. There are two types of gifts that Diksha wants from Taum: one is black and the other is white. To make her happy, Taum has to buy b black gifts and white gifts.
Determine the minimum cost of Diksha’s gifts. Example b=3 w=5 bc=3 wc=4 z=1 He can buy a black gift for 3 and convert it to a white gift for 1, making the total cost of each white gift 4. That matches the cost of a white gift, so he can do that or just buy black gifts and white gifts. Either way, the overall cost is 3*3+5*4=29. Function Description Complete the function taumBday in the editor below. It should return the minimal cost of obtaining the desired gifts. taumBday has the following parameter(s):
Returns
Input Format The first line will contain an integer t, the number of test cases. The next t pairs of lines are as follows: – The first line contains the values of integers b and w. – The next line contains the values of integers bc,wc , and z. Constraints 1<=t<=10 0<=b,w,b,wc,z<=10^9 Output Format lines, each containing an integer: the minimum amount of units Taum needs to spend on gifts. Sample Input STDIN Function ----- -------- 5 t = 5 10 10 b = 10, w = 10 1 1 1 bc = 1, wc = 1, z = 1 5 9 b = 5, w = 5 2 3 4 bc = 2, wc = 3, z = 4 3 6 b = 3, w = 6 9 1 1 bc = 9, wc = 1, z = 1 7 7 b = 7, w = 7 4 2 1 bc = 4, wc = 2, z = 1 3 3 b = 3, w = 3 1 9 2 bc = 1, wc = 9, z = 2Sample Output 20 37 12 35 12Organizing Containers of Balls Solution in C++#include |