| 1 | | def total(basket: list[int]) -> int: # in cents |
| 2 | | pass |
| 1 | from collections import Counter |
| 2 | from itertools import combinations |
| 3 | |
| 4 | |
| 5 | def total(basket: list[int]) -> int: # in cents |
| 6 | # Edge Case: Empty basket |
| 7 | if not basket: |
| 8 | return 0 |
| 9 | |
| 10 | # Count the frequency of each book |
| 11 | book_counts = Counter(basket) |
| 12 | frequencies = list(book_counts.values()) |
| 13 | |
| 14 | # Edge Case: All books are the same |
| 15 | if len(book_counts) == 1: |
| 16 | return len(basket) * 800 |
| 17 | |
| 18 | # Price calculation for different group sizes |
| 19 | discounts = {0: 0, 1: 0, 2: 5, 3: 10, 4: 20, 5: 25} |
| 20 | |
| 21 | # Generate all possible ways to group books for maximum discount |
| 22 | min_price = float('inf') |
| 23 | |
| 24 | # Try different combinations of group sizes |
| 25 | def calculate_price(groups): |
| 26 | total_cents = 0 |
| 27 | for group_size in groups: |
| 28 | total_cents += group_size * 800 * (100 - discounts[group_size]) // 100 |
| 29 | return total_cents |
| 30 | |
| 31 | # Generate all possible groupings |
| 32 | def generate_groupings(freqs): |
| 33 | # This is a recursive approach to find all possible groupings |
| 34 | if not any(freqs): |
| 35 | return [[]] |
| 36 | |
| 37 | # Find the highest frequency |
| 38 | max_freq = max(freqs) |
| 39 | groupings = [] |
| 40 | |
| 41 | # Try all possible group sizes from 1 to min(5, number of unique books) |
| 42 | unique_books = sum(1 for f in freqs if f > 0) |
| 43 | |
| 44 | for group_size in range(1, min(5, unique_books) + 1): |
| 45 | # Get indices of books with frequency > 0 |
| 46 | available_indices = [i for i, f in enumerate(freqs) if f > 0] |
| 47 | |
| 48 | # Try all combinations of 'group_size' books from available books |
| 49 | for indices in combinations(available_indices, group_size): |
| 50 | # Create a new frequency list after taking one copy of each selected book |
| 51 | new_freqs = freqs[:] |
| 52 | for i in indices: |
| 53 | new_freqs[i] -= 1 |
| 54 | |
| 55 | # Recursively generate groupings for the remaining books |
| 56 | sub_groupings = generate_groupings(new_freqs) |
| 57 | for sub_grouping in sub_groupings: |
| 58 | groupings.append([group_size] + sub_grouping) |
| 59 | |
| 60 | return groupings |
| 61 | |
| 62 | # Normalize frequencies to work with indices |
| 63 | all_groupings = generate_groupings(frequencies) |
| 64 | |
| 65 | # Edge Case: No valid groupings (should not happen with valid input) |
| 66 | if not all_groupings: |
| 67 | return sum(f * 800 for f in frequencies) |
| 68 | |
| 69 | for grouping in all_groupings: |
| 70 | price = calculate_price(grouping) |
| 71 | min_price = min(min_price, price) |
| 72 | |
| 73 | # Edge Case: Calculation resulted in infinity (should not happen) |
| 74 | if min_price == float('inf'): |
| 75 | return sum(f * 800 for f in frequencies) |
| 76 | |
| 77 | return min_price |
| 78 | |
| 79 | # Handled Edge Cases: Empty basket, all books are the same, no valid groupings, calculation resulted in infinity |