Java | TreeMap | 10 lines | Beats > 95% | Clean code
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$ for the `get` method, where n = number of snaps generated so far. $$O(1)$$ for all other methods.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(m*n)$$ where m = length of the snapshot array, n = number of snaps generated.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass SnapshotArray {\n  TreeMap<Integer, Integer>[] map;\n  int snap_id;\n\n  public SnapshotArray(int length) {\n    map = new TreeMap[length];\n    snap_id = 0;\n  }\n  \n  public void set(int index, int val) {\n    if (map[index] == null) map[index] = new TreeMap<>();\n\n    map[index].put(snap_id, val);\n  }\n  \n  public int snap() {\n    return snap_id++;\n  }\n  \n  public int get(int index, int snap_id) {\n    if (map[index] == null) return 0;\n    \n    var entry = map[index].floorEntry(snap_id);\n\n    return entry == null ? 0 : entry.getValue();\n  }\n}\n```\nIf you like my solution, please upvote it!
['Array', 'Hash Table', 'Ordered Map', 'Java']