In PySpark, drop duplicates while retaining the latest record based on a timestamp column.
Asked in the technical stage. Implement the equivalent transformation as a Python function over a list of record dictionaries.
records is a list of dictionaries containing the fields in key_columns and an integer timestamp. Return one record per unique key, retaining the record with the greatest timestamp. If timestamps tie, retain the earliest input record. Return retained records ordered by descending timestamp, preserving input order for ties.
def retain_latest_records(records, key_columns):