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. ๐
๐งฉ Problem Description
๐ 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 = 7Example 2
๐ Constraints
โ
My Approach
Greedy + Sorting Strategy
๐ Time and Auxiliary Space Complexity
๐งโ๐ป Code (C++)
โ Code (Java)
๐ Code (Python)
๐ง Contribution and Support
๐Visitor Count
Last updated