Posted on

Description

Submission

class SnapshotArray {
    vector<map<int,int,greater<int>>> sArray;
    vector<int> snapshot;
    int time;

    int getTimeInc() {
        return ++time;
    }

    int getTime() {
        return time;
    }
public:
    SnapshotArray(int length) {
        time = 0;
        sArray.resize(length);
        int t = getTime();
        for(int i = 0; i < sArray.size(); ++i) {
            sArray[i].insert({t, 0});
        }
    }
    
    void set(int index, int val) {
        sArray[index].insert({getTimeInc(), val});
    }
    
    int snap() {
        snapshot.push_back(getTime());
        
        return snapshot.size() - 1;
    }
    
    int get(int index, int snap_id) {
        int t = snapshot[snap_id];
        
        auto& m = sArray[index];
        auto iter = m.lower_bound(t);
        return iter->second;
    }
};

/**
 * Your SnapshotArray object will be instantiated and called as such:
 * SnapshotArray* obj = new SnapshotArray(length);
 * obj->set(index,val);
 * int param_2 = obj->snap();
 * int param_3 = obj->get(index,snap_id);
 */

Leave a Reply

Your email address will not be published. Required fields are marked *