|
| 1 | +package LeetCodeJava.Design; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/snapshot-array/description/ |
| 4 | + |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.TreeMap; |
| 8 | + |
| 9 | +/** |
| 10 | + * 1146. Snapshot Array |
| 11 | + * Medium |
| 12 | + * Topics |
| 13 | + * Companies |
| 14 | + * Hint |
| 15 | + * Implement a SnapshotArray that supports the following interface: |
| 16 | + * |
| 17 | + * SnapshotArray(int length) initializes an array-like data structure with the given length. Initially, each element equals 0. |
| 18 | + * void set(index, val) sets the element at the given index to be equal to val. |
| 19 | + * int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1. |
| 20 | + * int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_id |
| 21 | + * |
| 22 | + * |
| 23 | + * Example 1: |
| 24 | + * |
| 25 | + * Input: ["SnapshotArray","set","snap","set","get"] |
| 26 | + * [[3],[0,5],[],[0,6],[0,0]] |
| 27 | + * Output: [null,null,0,null,5] |
| 28 | + * Explanation: |
| 29 | + * SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3 |
| 30 | + * snapshotArr.set(0,5); // Set array[0] = 5 |
| 31 | + * snapshotArr.snap(); // Take a snapshot, return snap_id = 0 |
| 32 | + * snapshotArr.set(0,6); |
| 33 | + * snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5 |
| 34 | + * |
| 35 | + * |
| 36 | + * Constraints: |
| 37 | + * |
| 38 | + * 1 <= length <= 5 * 104 |
| 39 | + * 0 <= index < length |
| 40 | + * 0 <= val <= 109 |
| 41 | + * 0 <= snap_id < (the total number of times we call snap()) |
| 42 | + * At most 5 * 104 calls will be made to set, snap, and get. |
| 43 | + * |
| 44 | + */ |
| 45 | +public class SnapshotArray { |
| 46 | + |
| 47 | + /** |
| 48 | + * Your SnapshotArray object will be instantiated and called as such: |
| 49 | + * SnapshotArray obj = new SnapshotArray(length); |
| 50 | + * obj.set(index,val); |
| 51 | + * int param_2 = obj.snap(); |
| 52 | + * int param_3 = obj.get(index,snap_id); |
| 53 | + */ |
| 54 | + // V0 |
| 55 | + // (TLE) : TODO : optimize |
| 56 | +// class SnapshotArray_ { |
| 57 | +// |
| 58 | +// Integer[] elements; |
| 59 | +// Map<Integer, Integer[]> snapshotMap; |
| 60 | +// Integer snapshotCount; |
| 61 | +// |
| 62 | +// public SnapshotArray_(int length) { |
| 63 | +// this.elements = new Integer[length]; |
| 64 | +// this.snapshotMap = new HashMap<>(); |
| 65 | +// this.snapshotCount = 0; |
| 66 | +// // Store the initial snapshot (snapshot 0) |
| 67 | +// this.snapshotMap.put(this.snapshotCount, this.elements.clone()); |
| 68 | +// } |
| 69 | +// |
| 70 | +// public void set(int index, int val) { |
| 71 | +// // Set value in the current snapshot (current version of elements) |
| 72 | +// this.elements[index] = val; |
| 73 | +// } |
| 74 | +// |
| 75 | +// public int snap() { |
| 76 | +// // Take a snapshot of the current elements array by creating a new copy |
| 77 | +// snapshotMap.put(snapshotCount, elements.clone()); |
| 78 | +// // Increment snapshotCount to prepare for the next snapshot |
| 79 | +// return snapshotCount++; |
| 80 | +// } |
| 81 | +// |
| 82 | +// public int get(int index, int snap_id) { |
| 83 | +// // Retrieve the value from the snapshot with the given snap_id |
| 84 | +// return snapshotMap.get(snap_id)[index]; |
| 85 | +// } |
| 86 | +// } |
| 87 | + |
| 88 | + // V0_ |
| 89 | + // IDEA : (fix by GPT) |
| 90 | + /** |
| 91 | + * Key Optimizations |
| 92 | + * |
| 93 | + * 1. Sparse Storage: |
| 94 | + * •Instead of copying the whole array for each snapshot, |
| 95 | + * the TreeMap for each index only stores the values that |
| 96 | + * have been set at different snapshot versions. |
| 97 | + * This avoids unnecessary duplication of data. |
| 98 | + * |
| 99 | + * 2. Efficient Retrieval: |
| 100 | + * •Using TreeMap.floorEntry() allows us to efficiently |
| 101 | + * retrieve the value of an index at the given snapshot, |
| 102 | + * or the most recent value before the snapshot. |
| 103 | + * |
| 104 | + * 3. Memory Efficiency: |
| 105 | + * •The memory usage is optimized because we store |
| 106 | + * only the changes at each snapshot. If no change |
| 107 | + * occurs for an element, we do not store multiple |
| 108 | + * copies of the same value. |
| 109 | + * |
| 110 | + * -> This approach ensures that both set() and get() operations |
| 111 | + * remain efficient, with logarithmic time complexity due to |
| 112 | + * the use of TreeMap, while significantly reducing memory |
| 113 | + * usage compared to the original approach. |
| 114 | + * |
| 115 | + */ |
| 116 | + class SnapshotArray_0_1 { |
| 117 | + |
| 118 | + private int snapId; |
| 119 | + private Map<Integer, TreeMap<Integer, Integer>> snapshots; |
| 120 | + |
| 121 | + public SnapshotArray_0_1(int length) { |
| 122 | + this.snapId = 0; |
| 123 | + this.snapshots = new HashMap<>(); |
| 124 | + for (int i = 0; i < length; i++) { |
| 125 | + snapshots.put(i, new TreeMap<>()); |
| 126 | + snapshots.get(i).put(0, 0); // Initially, every element is 0 at snap_id 0 |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public void set(int index, int val) { |
| 131 | + snapshots.get(index).put(snapId, val); |
| 132 | + } |
| 133 | + |
| 134 | + public int snap() { |
| 135 | + return snapId++; |
| 136 | + } |
| 137 | + |
| 138 | + public int get(int index, int snap_id) { |
| 139 | + // Get the greatest key less than or equal to snap_id |
| 140 | + return snapshots.get(index).floorEntry(snap_id).getValue(); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // V1 |
| 145 | + // IDEA : Binary Search |
| 146 | + // https://leetcode.com/problems/snapshot-array/editorial/ |
| 147 | + class SnapshotArray_1_1 { |
| 148 | + int snapId = 0; |
| 149 | + TreeMap<Integer, Integer>[] historyRecords; |
| 150 | + |
| 151 | + public SnapshotArray_1_1(int length) { |
| 152 | + historyRecords = new TreeMap[length]; |
| 153 | + for (int i = 0; i < length; i++) { |
| 154 | + historyRecords[i] = new TreeMap<Integer, Integer>(); |
| 155 | + historyRecords[i].put(0, 0); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public void set(int index, int val) { |
| 160 | + historyRecords[index].put(snapId, val); |
| 161 | + } |
| 162 | + |
| 163 | + public int snap() { |
| 164 | + return snapId++; |
| 165 | + } |
| 166 | + |
| 167 | + public int get(int index, int snapId) { |
| 168 | + return historyRecords[index].floorEntry(snapId).getValue(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // V2 |
| 173 | +} |
0 commit comments