The cheapest bill
- O(n log n) · Hard
- Full plan
- Python
- JavaScript
- sorting
- lists
- indexes
Problem
On Saturday at the market, the fruit seller offers you an odd deal: you tell him how many kilos of each fruit you are taking, he gives you a list of prices per kilo, and you decide which price goes with which fruit. You want to pay as little as you can.
Write a function that takes quantities and prices, two lists of positive integers of the same length (they may be empty), and returns an integer: the lowest total possible. Each quantity is multiplied by one price, and each price is used exactly once.
With quantities 2 and 5 and prices 10 and 4: 5 kilos at 4 and 2 at 10 make 40; the other way, 5 at 10 and 2 at 4 make 58. The answer is 40. With empty lists you pay nothing: you return 0.
Examples
The example
[2, 5], [10, 4] → 40
Three fruits
[2, 5, 3], [4, 10, 7] → 61
The heaviest with the cheapest
[1, 8], [2, 9] → 25
A single fruit
[4], [6] → 24
Equal prices
[3, 1], [5, 5] → 20
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def bill(quantities, prices):
passJavaScript
function bill(quantities, prices) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.