Logo

Brendan's Blog

Come for computer science and tech related blog posts.

Brendan Lichtler

1-Minute Read

Desert Scene

https://leetcode.com/problems/time-based-key-value-store/

Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key’s value at a certain timestamp.

Implement the TimeMap class:

TimeMap() Initializes the object of the data structure. void set(String key, String value, int timestamp) Stores the key key with the value value at the given time timestamp. String get(String key, int timestamp) Returns a value such that set was called previously, with timestamp_prev <= timestamp. If there are multiple such values, it returns the value associated with the largest timestamp_prev. If there are no values, it returns “”.

Idea:

Solution:


class TimeMap {
public:
    unordered_map<string, map<int, string>> s;

    TimeMap() {

    }
    
    void set(string key, string value, int timestamp) {
        s[key][timestamp] = value;
    }
    
    string get(string key, int timestamp) {
        auto it = s[key].upper_bound(timestamp);

        if (it == s[key].begin()) {
            return "";
        } 
        return (--it)->second;
    }
};

Complexity Analysis:


Time

Set is O(1) constant time. O(N) worst case if the map resizes, amortized O(1)

Get is O(log(N)) for upper bound on map using binary search

Memory

O( N * T ) where N is number of keys and T is max number of timestamps for a key

Say Something

Comments

Nothing yet.

Recent Posts

Categories

About

Blog designed for tech and computer science content.