Implement a prototype of a simple cache query handler.
There are n data entries stored in the cache.
Each entry is of the form {timestamp, key, value}, where timestamp represents the time at which the entry was stored in the cache, key represents the ID assigned to the cache entry, and value represents the data value of the entry, an integer represented as a string.
The keys assigned to the cache entries may not be unique.
The cache query handler receives q query requests, where each query is of the form {key, timestamp}, where key represents the ID of the cache entry to find, and timestamp represents the time the entry was added.
Given two 2D arrays of strings, cache_entries and queries, of sizes n x 3 and q x 2 respectively, return an array of size q with the data values for each query.
Complete the getQueryAnswers function below.
The function is expected to return an INTEGER_ARRAY.
The function accepts following parameters:
2D_STRING_ARRAY cache_entries2D_STRING_ARRAY queries
Example
cache_entries = [["12:30:22", "a2er5i80", "125"], ["09:07:47", "io09ju56", "341"], ["01:23:09", "a2er5i80", "764"]]
queries = [["a2er5i80", "01:23:09"], ["io09ju56", "09:07:47"]]
queries[0]corresponds to the data entry at index 2, with value ="764"queries[1]corresponds to the data entry at index 1, with value ="341"
Return [764, 341].
这道题的核心是把缓存中的每条记录视为一个可快速检索的键值映射。由于查询同时给出了 <code>key</code> 和 <code>timestamp</code>,最直接的做法是先将所有 <code>cache_entries</code> 建成一个哈希表,键可以用 <code>(key, timestamp)</code> 组合,值保存对应的字符串整数。这样每个查询都能在接近 O(1) 的时间内找到结果。题目重点不在复杂算法,而在于正确处理“同一个 key 可能出现多次”的情况:必须同时匹配时间戳,才能定位到唯一的缓存条目。