LRU Cache Snapshot Printer
Problem statement
Process operations on a fixed-capacity LRU cache. PUT key value inserts or updates an entry and contributes null. GET key contributes its value, or the empty string when absent, and makes a hit most recently used. Evict the least recently used entry after an overflowing PUT.
SNAPSHOT contributes a compressed representation from most to least recent. Scan entries in that order, group keys with equal values, keep value groups in first-appearance order, and format each group as value:[key1,key2], joined with semicolons.
Function
runCacheSnapshots(capacity: int, operations: String[][]) → String[]Examples
Example 1
capacity = 2operations = [["PUT","A","2"],["PUT","B","2"],["SNAPSHOT"],["GET","A"],["SNAPSHOT"]]return = ["null","null","2:[B,A]","2","2:[A,B]"]Snapshot order follows recency, and a GET moves A to the front.
Example 2
capacity = 2operations = [["PUT","A","x"],["PUT","B","y"],["PUT","C","x"],["GET","A"],["SNAPSHOT"]]return = ["null","null","null","","x:[C];y:[B]"]A is evicted; groups retain the first value appearance in recency order.
Constraints
1 <= capacity <= 10001 <= operations.length <= 100000- Keys and values contain no comma, colon, brackets, or semicolon.