from collections import defaultdict _ = input() nums = [int(x) for x in input().split(" ") if (x != " " and x != "")] def solve_1b(nums): counts = defaultdict(int) for num in nums: counts[num] += 1 sorted_leaders = sorted(counts.items(), key=lambda x: x[1], reverse=True) n_segments = 0 n_currently_assigned_elems = 0 for elem in sorted_leaders: key, val = elem val -= 1 n_currently_assigned_elems += val n_currently_assigned_elems += counts[key] n_segments += 1 if n_currently_assigned_elems >= len(nums): break return n_segments print(solve_1b(nums))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | from collections import defaultdict _ = input() nums = [int(x) for x in input().split(" ") if (x != " " and x != "")] def solve_1b(nums): counts = defaultdict(int) for num in nums: counts[num] += 1 sorted_leaders = sorted(counts.items(), key=lambda x: x[1], reverse=True) n_segments = 0 n_currently_assigned_elems = 0 for elem in sorted_leaders: key, val = elem val -= 1 n_currently_assigned_elems += val n_currently_assigned_elems += counts[key] n_segments += 1 if n_currently_assigned_elems >= len(nums): break return n_segments print(solve_1b(nums)) |