Leetcode Problem 2144. Minimum Cost of Buying Candies With Discount
2144. Minimum Cost of Buying Candies With Discount
AI Mock Interview
Leetcode Solutions
Greedy Approach with Sorting
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Sort the array of candy costs in non-increasing order.
Initialize a variable
total_cost
to 0 to keep track of the total cost.
Iterate through the sorted array with an index
i
.
For every index
i
that is not a multiple of 3 (since the third candy is free), add the cost of the candy at index
i
to
total_cost
.
Return the value of
total_cost
as the minimum cost to buy all candies.
Counting Sort with Greedy Approach
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...