You are a gift packer preparing for a large event. You are given an array items of size N, where each element represents a type of gift. You need to select a collection of gifts to pack based on a special set of rules.
The rules for packing are:
Your goal is to maximize the total number of individual items packed. Return this maximum number..
The first line of input contains an integer N, the total number of items available.
The second line contains N space-separated integers, representing the types of the available items.
The output should be a single integer representing the maximum total number of items that can be packed according to the rules.
The first step is to count the frequency of each type of gift available.
The problem then becomes selecting a unique, positive "packed frequency" for each gift type, such that the packed frequency is less than or equal to its available frequency, and the sum of these packed frequencies is maximized.
A greedy approach is effective: process the gift types from most frequent to least frequent. For each type, try to pack as many items as possible (up to its available frequency) while ensuring the packed frequency has not been used by another type.
6
1 2 2 3 3 3
6
Type 3 (3 available): We can pack 3 items. The frequency 3 has not been used yet. Total packed = 3. Used frequencies: {3}.
Type 2 (2 available): We can pack 2 items. The frequency 2 has not been used. Total packed = 3 + 2 = 5. Used frequencies: {3, 2}.
Type 1 (1 available): We can pack 1 item. The frequency 1 has not been used. Total packed = 5 + 1 = 6. Used frequencies: {3, 2, 1}.
The maximum number of items is 6.
The expected time complexity is O(N + U log U), where N is the number of items and U is the number of unique item types.
1 <= N <= 10^5
1 <= items[i] <= 10^9
Time limit: 1sec