Latest records
MediumOnline Assessment
Keep only the most recent record for each key.
Each record is [key, timestamp, value]. For every distinct key, keep the record with the largest timestamp. If several records for a key share that largest timestamp, keep the one that appears latest in the input.
Input: records, a list of [key, timestamp, value] where key and value are strings and timestamp is an integer.
Output: the kept records, one per key, as [key, timestamp, value] lists sorted by key in ascending lexicographic order. If there are no records, return [].
Example 1
- Input:
- [[["b",2,"old"],["a",3,"x"],["b",2,"new"]]]
- Output:
- [["a",3,"x"],["b",2,"new"]]
- Explanation
- Key "a" has one record. Key "b" has two records with timestamp 2; they tie, so the one later in the input ("new") wins. Output is sorted by key.
Example 2
- Input:
- [[["c",1,"p"],["a",8,"x"],["a",3,"y"],["c",4,"q"]]]
- Output:
- [["a",8,"x"],["c",4,"q"]]
- Explanation
- For "a", timestamp 8 beats 3 even though the 3 appears later in the input. For "c", timestamp 4 beats 1. Output is sorted: a, then c.
Example 3
- Input:
- [[["k",7,"a"],["k",7,"b"],["k",7,"c"]]]
- Output:
- [["k",7,"c"]]
- Explanation
- All three records tie on timestamp 7, so the last one in the input, value "c", is kept.
Constraints
0 <= records.length <= 10,000keyandvalueare non-empty strings of lowercase letters-1,000,000 <= timestamp <= 1,000,000