🔥2 Easy To Understand C++🔥 Solution|| Fastest 🔥🔥
\n# Approach : HashMap\nCreate a Unique Hashmap\n Describe your approach to solving the problem. \n\n# Code 1\n\nclass SnapshotArray {\npublic:\n\nvector<map<in
\n# Approach : HashMap\nCreate a Unique Hashmap\n<!-- Describe your approach to solving the problem. -->\n\n# Code 1\n```\nclass SnapshotArray {\npublic:\n\nvector<map<int,int ,greater<int>>> mp;\nint id=0;\n    SnapshotArray(int length) {\n        mp.resize(length);\n    }\n    \n    void set(int index, int val) {\n        mp[index][id]=val;\n    }\n    \n    int snap() {\n    return id++;\n}\n    \n    int get(int index, int snap_id) {\n        auto i=mp[index].lower_bound(snap_id);\n        return i->second;\n    }\n};\n\n/**\n * Your SnapshotArray object will be instantiated and called as such:\n * SnapshotArray* obj = new SnapshotArray(length);\n * obj->set(index,val);\n * int param_2 = obj->snap();\n * int param_3 = obj->get(index,snap_id);\n */\n```\n\n# Approach : Binary Search\n\n<!-- Describe your approach to solving the problem. -->\n\n# Code 1\n```\nclass SnapshotArray {\npublic:\n    SnapshotArray(int length) : len(length) {\n        \n    }\n    \n    void set(int index, int val) {\n        if (index >= len) {\n            return;\n        }\n                \n        snapArray[index][snapCount] = val;\n    }\n    \n    int snap() {\n        ++snapCount;\n        return snapCount - 1;\n    }\n    \n    int get(int index, int snap_id) {\n        if (index >= len || snap_id > snapCount) {\n            return -1;\n        }\n        if (snapArray[index].empty()) {\n            return 0;\n        }\n        auto pos = snapArray[index].lower_bound(snap_id);\n        if (pos == snapArray[index].end()) {\n            return 0;\n        }\n\n        return pos->second;\n    }\n    private:\n    using SMap = std::map<int, int, greater<int>>;\n    std::unordered_map<int, SMap> snapArray;\n    int snapCount = 0;\n    int len = 0;\n};\n\n/**\n * Your SnapshotArray object will be instantiated and called as such:\n * SnapshotArray* obj = new SnapshotArray(length);\n * obj->set(index,val);\n * int param_2 = obj->snap();\n * int param_3 = obj->get(index,snap_id);\n */\n```
['Array', 'Hash Table', 'Binary Search', 'C++']