AlgoMaster Logo

Snapshot Array

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Naive Approach

Intuition:

The naive approach involves storing the entire array for each snapshot. Although this method is simple to implement, it is not efficient as it uses a lot of memory space if the array or the number of snapshots is large.

Code:

Intuition:

Instead of storing the entire array for each snapshot, we only store the changes made for each snapshot. This can be efficiently handled using a list of TreeMaps where the keys are indices of the array, and the values are another TreeMap that maps the snapshot id to the values that were set during that snapshot.

Code: