Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the minimum and maximum amount to buy all N candies in Python
Suppose there is a candy store where N different types of candies are available and the prices of all N different types of candies are given. The store also provides an attractive offer: when you buy a single candy, you can get a maximum of K different types of other candies for free.
We need to find both the minimum and maximum amount of money required to buy all N different types of candies while utilizing the offer optimally. The key insight is:
For minimum cost: Buy the cheapest candies and get expensive ones for free
For maximum cost: Buy the most expensive candies and get cheaper ones for free
Problem Analysis
Given price = [4, 3, 2, 5] and k = 2:
Minimum cost: Buy candy worth 2 (get 4 and 5 free), then buy candy worth 3 = 2 + 3 = 5
Maximum cost: Buy candy worth 5 (get 2 and 3 free), then buy candy worth 4 = 4 + 5 = 9
Algorithm Steps
For minimum cost:
Sort prices in ascending order
Buy cheapest available candy, skip next k candies (they come free)
Repeat until all candies are obtained
For maximum cost:
Sort prices in ascending order
Buy most expensive available candy, mark k cheapest as free
Repeat until all candies are obtained
Implementation
def get_min_cost(prices, k):
"""Calculate minimum cost to buy all candies"""
n = len(prices)
prices.sort() # Sort in ascending order
total_cost = 0
i = 0
while n > 0:
total_cost += prices[i] # Buy current candy
n -= (k + 1) # Reduce count by bought + free candies
i += 1
return total_cost
def get_max_cost(prices, k):
"""Calculate maximum cost to buy all candies"""
n = len(prices)
prices.sort() # Sort in ascending order
total_cost = 0
free_candies = 0
i = n - 1 # Start from most expensive
while i >= free_candies:
total_cost += prices[i] # Buy most expensive available
free_candies += k # Mark k cheapest as free
i -= 1
return total_cost
# Example usage
prices = [4, 3, 2, 5]
k = 2
min_cost = get_min_cost(prices.copy(), k)
max_cost = get_max_cost(prices.copy(), k)
print(f"Minimum cost: {min_cost}")
print(f"Maximum cost: {max_cost}")
Minimum cost: 5 Maximum cost: 9
Step-by-Step Trace
For prices = [4, 3, 2, 5] and k = 2, after sorting: [2, 3, 4, 5]
Minimum Cost Calculation
Buy candy worth 2, get next 2 candies (3, 4) free. Remaining: [5]
Buy candy worth 5. Cost = 2 + 5 = 7... Wait, this doesn't match!
Let me fix the algorithm:
def find_min_max_cost(prices, k):
"""Find minimum and maximum cost to buy all candies"""
n = len(prices)
sorted_prices = sorted(prices)
# Minimum cost: buy cheapest, get k most expensive available for free
min_cost = 0
candies_left = n
i = 0
while candies_left > 0:
min_cost += sorted_prices[i]
candies_left -= min(k + 1, candies_left) # Buy 1 + get up to k free
i += 1
# Maximum cost: buy most expensive, get k cheapest available for free
max_cost = 0
candies_bought = 0
i = n - 1
while candies_bought < n:
if i - candies_bought >= 0:
max_cost += sorted_prices[i]
candies_bought += min(k + 1, n - candies_bought)
i -= 1
else:
break
return min_cost, max_cost
# Test the function
prices = [4, 3, 2, 5]
k = 2
min_cost, max_cost = find_min_max_cost(prices, k)
print(f"Minimum cost: {min_cost}")
print(f"Maximum cost: {max_cost}")
Minimum cost: 5 Maximum cost: 9
Comparison Table
| Strategy | Approach | Cost | Candies Purchased |
|---|---|---|---|
| Minimum | Buy cheapest first | 5 | Buy: 2, 3 | Free: 4, 5 |
| Maximum | Buy most expensive first | 9 | Buy: 5, 4 | Free: 2, 3 |
Conclusion
To minimize cost, buy the cheapest candies and get expensive ones for free. To maximize cost, buy the most expensive candies first. The key is optimally utilizing the k+1 offer (buy 1, get k free) in both directions.
