Thủ Thuật Hướng dẫn Organizing containers of balls hackerrank solution 2022 2022
Quý khách đang tìm kiếm từ khóa Organizing containers of balls hackerrank solution 2022 được Update vào lúc : 2022-05-24 18:40:00 . Với phương châm chia sẻ Bí kíp về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi Read tài liệu vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Mình lý giải và hướng dẫn lại nha.
Pro đang tìm kiếm từ khóa Organizing containers of balls hackerrank solution được Update vào lúc : 2022-05-24 18:40:06 . Với phương châm chia sẻ Thủ Thuật Hướng dẫn trong nội dung nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi Read nội dung nội dung bài viết vẫn ko hiểu thì hoàn toàn hoàn toàn có thể lại phản hồi ở cuối bài để Mình lý giải và hướng dẫn lại nha.
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:
Organizing Containers of Balls Solution in C++
Organizing Containers of Balls Solution in Java
Organizing Containers of Balls Solution in Python
Organizing Containers of Balls Solution in C#
ball types | red | blue | green
containers | | |
———————+—–+——+——-
A | 2 | 3 | 1
B | 4 | 0 | 1
C | 1 | 3 | 3
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 | 5
Now 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.
The cost of each black gift is bc units.
The cost of every white gift is wc units.
The cost to convert a black gift into white gift or vice versa is units.
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):
int b: the number of black gifts
int w: the number of white gifts
int bc: the cost of a black gift
int wc: the cost of a white gift
int z: the cost to convert one color gift to the other color
Returns
int: the minimum cost to purchase the gifts
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 = 2
Sample Output
20 37 12 35 12
Organizing Containers of Balls Solution in C++
#include