Similar Problems
Similar Problems not available
Minimum Cost Of Buying Candies With Discount - Leetcode Solution
Companies:
LeetCode: Minimum Cost Of Buying Candies With Discount Leetcode Solution
Difficulty: Easy
Problem Statement:
You are given an array arr which represents the price of n candies. You are also given an integer m which represents the number of candies that you have to buy.
There is a special discount offer for the customers. The offer states that for every k candies you buy, you can get one candy for free.
Return the minimum cost it takes to buy m candies using the discount offer.
Solution:
To solve this problem, we can sort the array arr in non-decreasing order and then iterate over it. We can then use a sliding window approach to find the minimum cost of buying m candies.
Let's say we are currently considering the ith candy in the array, and we have already bought j candies. We can then calculate the cost of buying the next k candies, including the free candy we will get from the discount offer.
The cost of buying the next k candies would be equal to the sum of prices of candies from i to i+k-1 in the sorted array arr. Also, we can subtract the price of the (i+j)th candy from it as we will get it for free.
We can loop through all possible values of k (such that i+k-1 is less than or equal to n and k is a multiple of (k-1)) and find the minimum cost.
We can keep track of the minimum cost we have seen so far as we iterate over the array arr. Once we have iterated over the entire array arr, we will have the minimum cost of buying m candies using the discount offer.
Pseudo Code:
- Sort the given array arr in non-decreasing order.
- Initialize the minimum cost to be infinity.
- Loop over all possible values of i such that i+m-1 ≤ n.
- Initialize the current cost to be 0.
- Loop over all possible values of k such that i+k-1 ≤ n and k is a multiple of (k-1).
- Compute the cost of buying the next k candies, including the free candy we will get from the discount offer.
- If the current cost is less than the minimum cost, update the minimum cost.
- Return the minimum cost.
Time Complexity:
Sorting the array will take O(nlogn) time. Iterating over all possible values of i and k will take O(n^2) time. However, we can optimize the inner loop by jumping k steps at a time, which reduces the time complexity to O(nlogn). Thus, the overall time complexity of the algorithm is O(nlogn).
Space Complexity:
Sorting the array in-place will take O(1) space. Thus, the overall space complexity of the algorithm is O(1).
Full Python Code:
class Solution: def getMinimumCost(self, arr: List[int], m: int) -> int: n = len(arr) arr.sort() ans = float('inf') for i in range(n): curr_cost = 0 for k in range(1, m+1, k): if i+k > n: break curr_cost += sum(arr[i:i+k]) curr_cost -= arr[i+k-1] if curr_cost < ans: ans = curr_cost return ans
Note: This solution type is known as Brute Force. There are other better approachs available to solve this problem with improved time complexity.
Minimum Cost Of Buying Candies With Discount Solution Code
1