You are given an integer array prices representing the prices of various chocolates in a store. You are also given a single integer money, which represents your initial amount of money.
You must buy exactly two chocolates in such a way that you still have some non-negative leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.
Return the amount of money you will have leftover after buying the two chocolates. If there is no way for you to buy two chocolates without ending up in debt, return money. Note that the leftover must be non-negative.
Example 1:
Input: prices = [1,3,2], money = 5
Output: 2Explanation: Buy the two chocolates with prices 1 + 2. The leftover money is 5 - 3 = 2.
Example 2:
Input: prices = [2,5,1,2], money = 3
Output: 0Explanation: Buy the two chocolates with prices 1 + 2. The leftover money is 3 - 3 = 0.
Example 3:
Input: prices = [2,2,4,3], money = 2
Output: 2Explanation: There is no way to buy two chocolates. So, we return the given money 2.
Constraints:
2 <= prices.length <= 501 <= prices[i], money <= 100Before attempting this problem, you should be comfortable with:
We want to buy two chocolates and maximize the leftover money, which means we should minimize the total cost of the two chocolates. The brute force approach tries every possible pair of chocolates and keeps track of the maximum leftover (or equivalently, the pair with minimum total cost that we can afford).
-1 (indicating no valid purchase found yet).(i, j) where i < j:-1), return the original money (we buy nothing).class Solution:
def buyChoco(self, prices: List[int], money: int) -> int:
res = -1
for i in range(len(prices)):
for j in range(i + 1, len(prices)):
if prices[i] + prices[j] <= money:
res = max(res, money - prices[i] - prices[j])
return res if res != -1 else moneyTo maximize leftover money, we need to buy the two cheapest chocolates. After sorting the prices, the two cheapest chocolates will be at the beginning of the array. We just need to check if we can afford them.
We can find the two cheapest chocolates in a single pass without sorting. As we iterate through the prices, we maintain the two smallest values seen so far. This gives us the optimal pair to buy.
min1) and second smallest (min2) prices, both set to infinity.min1, update min2 to be the old min1, and min1 to be the current price.min2, update min2 to be the current price.min1 and min2 prices.If the sum of the two cheapest chocolates exceeds your money, you should return the original money (buy nothing), not a negative leftover or zero.
# Wrong: returning negative leftover
return money - min1 - min2 # Could be negative
# Correct: check if affordable firstWhen finding a new smallest price, forgetting to shift the old minimum to become the second minimum loses track of valid candidates.
# Wrong: losing second minimum
if p < min1:
min1 = p # min2 is never updated with old min1