At Acme Analytics, a Python batch job is timing out while processing a large list of event records. Each record is represented as a pair [record_id, value]. Multiple records may share the same record_id. Your task is to optimize the job by aggregating all values for the same record_id and returning the result sorted by record_id.
A naive solution scans the full list repeatedly for each ID, which is too slow on large datasets. Write an efficient function that processes the list in near-linear time.
records, a list of pairs [record_id, value], where both elements are integers.[record_id, total_value] such that each record_id appears once and total_value is the sum of all values associated with that ID, sorted in ascending order by record_id.Example 1
records = [[1, 3], [2, 5], [1, 4], [3, 2]][[1, 7], [2, 5], [3, 2]]1 appears twice, so its values are summed.Example 2
records = [[5, 10], [5, -3], [2, 8], [2, 1], [7, 4]][[2, 9], [5, 7], [7, 4]]1 <= len(records) <= 2 * 10^50 <= record_id <= 10^9-10^9 <= value <= 10^9