SellingProducts
SRM 379 · 2007-11-28 · by asal1
Problem Statement
You will be given
Constraints
- price will contain between 1 and 50 elements, inclusive.
- Each element of price will be between 1 and 10^6, inclusive.
- cost will contain the same number of elements as price.
- Each element of cost will be between 0 and 10^6, inclusive.
{13,22,35}
{0,0,0}
Returns: 22
If we sell the product at 13 then all three would buy it.(3x13=39) If we sell the product at 22 then only two would buy it. (2x22=44) If we sell the product at 35 then only one would buy it. (1x35=35) So, 22 is the optimal price for our product.
{13,22,35}
{5,15,30}
Returns: 13
If we sell the product at 13 then all three would buy it, but we would only sell to the first one.(13-5=8) If we sell the product at 22 then only two would buy it, but we would only sell to the second one.(22-15=7). If we sell the product at 35 then only one would buy it. (35-30=5) So, 13 is the optimal price for our product.
{13,22,35}
{15,30,40}
Returns: 0
Here it is too expensive to sell to anyone. So the optimal price is 0.
{10,10,20,20,5}
{1,5,11,15,0}
Returns: 10
If we sell the product at 10 we gain 9 from the first customer and 5 from the second one(Total profit = 14). If we sell the product at 20 we gain 9 from the third customer and 5 from the fourth one(Total profit = 14). So both 10 and 20 are optimal prices but we must choose the smallest one.
{13,17,14,30,19,17,55,16}
{12,1,5,10,3,2,40,19}
Returns: 17
Submissions are judged against all 133 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SellingProducts with a public method int optimalPrice(vector<int> price, vector<int> cost) · 133 test cases · 2 s / 256 MB per case