TTL cache

Write codeMedium25 min

Your task

Implement a TTL cache. Process operations of three shapes: ["set", now, key, value, ttl] stores key=value at time now with a time-to-live of ttl (the entry expires at now+ttl, i.e. it is alive for times strictly less than now+ttl), ["get", now, key] returns the value if the key exists and now < its expiry, else -1, and ["count", now] returns how many keys are currently alive at time now. Setting an existing key overwrites its value and resets its expiry. Timestamps are non-decreasing non-negative integers. Return the list of results from every get and count operation, in order.

Examples

ttl_cache(ops: list[list]) -> list[int]
Example 1
Inputttl_cache([["set", 0, "a", 10, 5], ["count", 2], ["get", 2, "a"], ["get", 5, "a"]])
Output[1, 10, -1]