Dsa
Greedy
Easy
Distributing Candies
You are tasked with distributing candies to children at a party in such a way that each child receives at least one candy. Given a list of children's sweetness preferences, your goal is to distribute candies such that the total sweetness of the candies distributed is maximized, while ensuring that each child receives a different amount of sweetness.
Write a function that takes as input a list of integers, where each integer represents a child's sweetness preference. Your function should output the maximum total sweetness you can distribute to the children.
Input:
- A single line containing integers `pref[0], pref[1], ..., pref[n-1]` (1 <= len(pref) <= 100 and 1 <= pref[i] <= 100).
Output:
- An integer representing the maximum total sweetness that can be distributed.
Example:
Input:
1 2 3 3 1
Output:
8
In this case, distributing candies with 1, 2, 3, 4, and 5 sweetness levels would maximize the total sweetness.
Key concepts
greedysortingarray
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now