12. Shop in Candy Store
โ GFG solution to the Shop in Candy Store problem: find minimum and maximum cost to buy all candies with free candy offer using greedy approach. ๐
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
In a candy store, there are different types of candies available and prices[i]
represent the price of ith
types of candies. You are provided with an attractive offer: For every candy you buy from the store, you can get up to k other different candies for free.
Your task is to find the minimum and maximum amount of money needed to buy all the candies.
Note: In both cases, you must take the maximum number of free candies possible during each purchase.
๐ Examples
Example 1
Input: prices[] = [3, 2, 1, 4], k = 2
Output: [3, 7]
Explanation:
- Minimum cost: Buy candy worth 1, get candies worth 3 and 4 for free. Also buy candy worth 2.
Total: 1 + 2 = 3
- Maximum cost: Buy candy worth 4, get candies worth 1 and 2 for free. Also buy candy worth 3.
Total: 3 + 4 = 7
Example 2
Input: prices[] = [3, 2, 1, 4, 5], k = 4
Output: [1, 5]
Explanation:
- Minimum cost: Buy candy with cost 1 and get all other candies for free. Total: 1
- Maximum cost: Buy candy with cost 5 and get all other candies for free. Total: 5
๐ Constraints
$1 \le \text{prices.size()} \le 10^5$
$0 \le k \le \text{prices.size()}$
$1 \le \text{prices}[i] \le 10^4$
โ
My Approach
The optimal approach uses Greedy Algorithm with Sorting to maximize the benefit from free candies:
Greedy + Sorting Strategy
Sort the Array:
Sort prices in ascending order to enable optimal greedy selection.
Minimum Cost Strategy:
Buy the cheapest candy and get k most expensive remaining candies for free.
Start from index 0 (cheapest), after each purchase, reduce remaining count by k.
Continue until all candies are covered.
Maximum Cost Strategy:
Buy the most expensive candy and get k cheapest remaining candies for free.
Start from last index (most expensive), after each purchase, increase lower bound by k.
Continue until all candies are covered.
Implementation Details:
Use remaining count tracking for minimum cost calculation.
Use index boundary tracking for maximum cost calculation.
๐ Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n), where n is the size of the prices array. The sorting step dominates the complexity, while the two loops run in O(n) time combined.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables like indices, costs, and bounds.
๐งโ๐ป Code (C++)
class Solution {
public:
vector<int> minMaxCandy(vector<int>& prices, int k) {
sort(prices.begin(), prices.end());
int n = prices.size(), min = 0, max = 0;
for (int i = 0, rem = n; i < rem; i++, rem -= k) min += prices[i];
for (int j = n - 1, idx = -1; j > idx; j--, idx += k) max += prices[j];
return {min, max};
}
};
โ Code (Java)
class Solution {
public ArrayList<Integer> minMaxCandy(int[] prices, int k) {
Arrays.sort(prices);
int n = prices.length, min = 0, max = 0;
for (int i = 0, rem = n; i < rem; i++, rem -= k) min += prices[i];
for (int j = n - 1, idx = -1; j > idx; j--, idx += k) max += prices[j];
return new ArrayList<>(Arrays.asList(min, max));
}
}
๐ Code (Python)
class Solution:
def minMaxCandy(self, prices, k):
prices.sort()
n, min_, max_ = len(prices), 0, 0
i, rem = 0, n
while i < rem:
min_ += prices[i]; i += 1; rem -= k
j, idx = n - 1, -1
while j > idx:
max_ += prices[j]; j -= 1; idx += k
return [min_, max_]
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Let's make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated