Stream Latest Log Versions
Learn this problemProblem statement
Process a finite sequence of log-stream operations from left to right. Each log is a pair containing an integer timestamp and a message. At most one version of each message is pending at a time.
- For an
addoperation, if the message is not pending, make the supplied log its pending version. If the message is already pending, replace its pending version only when the supplied timestamp is greater. An equal or smaller timestamp leaves the current version unchanged. - For a
getoperation, remove and return the pending version with the smallest timestamp. If multiple pending versions have the same timestamp, return the one whose message is lexicographically smaller. If no version is pending, returnEMPTY.
After a version is returned, that message is no longer pending. A later add for the same message starts a new pending lifecycle, regardless of the timestamp that was returned earlier. Only operations already processed are visible, and later operations never revise a returned result.
Return one string for each get, in operation order. Encode a returned log as timestamp|message. The timestamp and message entries at a get position are placeholders and are ignored.
Function
processLatestLogs(operations: String[], timestamps: int[], messages: String[]) → String[]Examples
Example 1
operations = ["add","add","add","get","get","add","get"]timestamps = [5,8,3,0,0,2,0]messages = ["build","build","deploy","","","build",""]return = ["3|deploy","8|build","2|build"]The timestamp 8 version replaces build at timestamp 5. The first two results are therefore 3|deploy and 8|build. Once build is returned, adding it at timestamp 2 starts a new lifecycle.
Example 2
operations = ["add","add","add","add","get","get","get"]timestamps = [4,4,4,2,0,0,0]messages = ["alpha","alpha","beta","alpha","","",""]return = ["4|alpha","4|beta","EMPTY"]The equal and smaller updates for alpha are ignored. Its timestamp ties with beta, so message order returns alpha first. The final get is empty.
Constraints
1 <= operations.length <= 10^5.operations.length == timestamps.length == messages.length.- Every operation is either
addorget. - At an
addposition,0 <= timestamps[i] <= 10^9. - At an
addposition,messages[i]contains between1and20lowercase English letters. - At a
getposition,timestamps[i]andmessages[i]are ignored.