The problem requires efficiently storing and retrieving values based on timestamps. Given the constraint that timestamps are strictly increasing for each key, we can leverage this property to maintain an ordered list of values for each key. This allows us to use binary search to quickly find the most recent value for a given timestamp.
Data structure:
- Use a
HashMap<String, List<ValueTimestampTuple>>
:- Key: The string key.
- Value: A list of
ValueTimestampTuple
, each containing a value and a timestamp.
Methods
-
set(String key, String value, int timestamp)
- Check for Key Existence: If the key does not exist in the map, initialize a new
ArrayList
for that key. - Store the Value: Append the
(value, timestamp)
tuple2 to the list associated with the key. Since timestamps are strictly increasing, this maintains the order.
- Check for Key Existence: If the key does not exist in the map, initialize a new
-
get(String key, int timestamp)
- Key Check: If the key does not exist in the map, return an empty string.
- Binary Search:
- Initialize
leftIndex
to0
andrightIndex
to the last index of the list. - If the first timestamp is greater than the given timestamp, return an empty string.
- If the last timestamp is less than or equal to the given timestamp, return the value at the last index.
- Perform binary search:
- Calculate
middleIndex
as(leftIndex + rightIndex) / 2
. - If the timestamp at
middleIndex
is less than or equal to the given timestamp, updatefoundIndex
tomiddleIndex
and moveleftIndex
tomiddleIndex + 1
. - If the timestamp at
middleIndex
is greater than the given timestamp, moverightIndex
tomiddleIndex - 1
.
- Calculate
- Return the value at
foundIndex
.
- Initialize
- Time Complexity:
set
:O(1)
on average, as appending to an ArrayList is amortized constant time.get
:O(log N)
, due to binary search.
- Space Complexity:
O(N)
, as each set operation adds a new entry to the list.
This solution efficiently handles the problem's requirements by using a combination of a HashMap
and an ArrayList
.
The set method ensures constant time storage of values, while the get method uses binary search to achieve logarithmic
time retrieval. This approach leverages the strictly increasing timestamps to maintain an ordered list, which is key to
efficient retrieval. Overall, the solution is optimal and well-suited for the constraints provided.