You are given a set of item prices. You can buy any number of these items. Find the largest amount of money that cannot be formed using combinations of these prices.
In other words, given a set of positive integers, find the largest integer that cannot be expressed as a non-negative integer linear combination of the elements of the set.
Example 1:
Input: prices = [2, 3]
Output: 1
Explanation:
We can form:
2, 3, 4, 5, 6, 7, 8, 9, 10, ...
We can form any number greater than 1.
Example 2:
Input: prices = [3, 5, 7]
Output: 4
Explanation:
We can form:
3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ...
We can form any number greater than 4.
Example 3:
Input: prices = [1, 5, 10]
Output: 0
Explanation:
We can form any positive integer. Since we can buy an item of price 1, we can form any amount. Thus, the largest amount that cannot be formed is 0.
Constraints:
1 <= prices.length <= 10
1 <= prices[i] <= 100
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the most expensive item that cannot be bought involves checking every single possible price. We systematically test each price to see if it can be formed by combining the prices of available items.
Here's how the algorithm would work step-by-step:
def find_most_expensive_unbuyable_item(item_prices):
most_expensive_unbuyable = 0
consecutive_buyable_count = 0
max_consecutive_buyable = 100 # Threshold for assuming all higher prices are buyable
current_price = 1
while consecutive_buyable_count < max_consecutive_buyable:
is_buyable = False
# Check if the current price can be formed by combinations of item prices
for i in range(2**(len(item_prices))):
combination_sum = 0
for j in range(len(item_prices)):
if (i >> j) & 1:
combination_sum += item_prices[j]
if combination_sum == current_price:
is_buyable = True
break
if is_buyable:
consecutive_buyable_count += 1
else:
# Update the most expensive unbuyable item if current price is not buyable
most_expensive_unbuyable = current_price
consecutive_buyable_count = 0
current_price += 1
return most_expensive_unbuyable
The problem asks us to find the largest value that cannot be created by combining a given set of item prices. We will use a mathematical trick to find a point where all numbers above it can be made, and therefore the solution is the largest number below it that can't.
Here's how the algorithm would work step-by-step:
def most_expensive_item_that_cannot_be_bought(item_prices):
minimum_price = min(item_prices)
if minimum_price == 1:
return -1
def can_make_price(target_price, available_prices):
if target_price == 0:
return True
if target_price < 0:
return False
for price in available_prices:
if can_make_price(target_price - price, available_prices):
return True
return False
# Start checking downwards from a reasonable upper bound.
start_price = minimum_price * max(item_prices)
consecutive_prices_that_can_be_made = 0
consecutive_prices_needed = minimum_price
for current_price in range(start_price, -1, -1):
if can_make_price(current_price, item_prices):
consecutive_prices_that_can_be_made += 1
# If we have reached the tipping point.
if consecutive_prices_that_can_be_made == consecutive_prices_needed:
# This is the number one less than the lowest consecutive value.
return current_price - 1
else:
# Reset the counter if the current price cannot be made.
consecutive_prices_that_can_be_made = 0
# If no solution is found, return -1 as a failsafe.
return -1
Case | How to Handle |
---|---|
Empty 'costs' array | Return 0, as no items are available, making it impossible to buy anything |
All costs are zero | Return 0, as no matter how much you have you can't buy anything, as if you can buy everything nothing will be left over |
All costs are the same positive value | Return the given money modulo the cost, since the only value left is whatever is less than the cost |
Money is zero | Return 0, as you can't buy anything with no money |
Costs contain negative numbers | Treat negative costs as if they were free, buy as many of those as possible and ignore them for the calculation of leftovers since those are effectively free |
Costs are very large numbers (potential overflow) | Use a long or similar data type to avoid integer overflow when summing costs if a summation is necessary |
Only one item, and money is less than the cost of that item | Return the amount of money available since nothing can be bought |
The maximum cost is significantly higher than the other costs | The solution should efficiently handle this skewed distribution by prioritizing purchasing items with lower costs |