|
2 | 2 |
|
3 | 3 | // https://leetcode.com/problems/snapshot-array/description/ |
4 | 4 |
|
5 | | -import java.util.HashMap; |
6 | | -import java.util.Map; |
7 | | -import java.util.TreeMap; |
| 5 | +import java.util.*; |
8 | 6 |
|
9 | 7 | /** |
10 | 8 | * 1146. Snapshot Array |
@@ -170,4 +168,84 @@ public int get(int index, int snapId) { |
170 | 168 | } |
171 | 169 |
|
172 | 170 | // V2 |
| 171 | + // IDEA : HASHMAP |
| 172 | + // https://leetcode.com/problems/snapshot-array/solutions/350574/java-python-3-3-codes-w-analysis-store-difference-by-hashmap-and-treemap-respectively/ |
| 173 | + class SnapshotArray_2_1 { |
| 174 | + |
| 175 | + private List<Map<Integer, Integer>> shot; |
| 176 | + private Map<Integer, Integer> diff; |
| 177 | + |
| 178 | + public SnapshotArray_2_1(int length) { |
| 179 | + shot = new ArrayList<>(length); |
| 180 | + diff = new HashMap<>(length); |
| 181 | + } |
| 182 | + |
| 183 | + public void set(int index, int val) { |
| 184 | + diff.put(index, val); |
| 185 | + } |
| 186 | + |
| 187 | + public int snap() { |
| 188 | + shot.add(diff); |
| 189 | + diff = new HashMap<>(); |
| 190 | + return shot.size() - 1; |
| 191 | + } |
| 192 | + |
| 193 | + public int get(int index, int snap_id) { |
| 194 | + for (int i = snap_id; i >= 0; --i) |
| 195 | + if (shot.get(i).containsKey(index)) |
| 196 | + return shot.get(i).get(index); |
| 197 | + return 0; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + // V2-2 |
| 202 | + // https://leetcode.com/problems/snapshot-array/solutions/350574/java-python-3-3-codes-w-analysis-store-difference-by-hashmap-and-treemap-respectively/ |
| 203 | + // IDEA : BINARY SEARCH |
| 204 | + class SnapshotArray_2_2 { |
| 205 | + private int snapId; |
| 206 | + private List<List<int[]>> shot; |
| 207 | + |
| 208 | + public SnapshotArray_2_2(int length) { |
| 209 | + this.snapId = 0; |
| 210 | + this.shot = new ArrayList<>(); |
| 211 | + // Initialize the list with [-1, 0] for each index |
| 212 | + for (int i = 0; i < length; i++) { |
| 213 | + List<int[]> snapshotList = new ArrayList<>(); |
| 214 | + snapshotList.add(new int[]{-1, 0}); // Add [-1, 0] as the initial state |
| 215 | + this.shot.add(snapshotList); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + public void set(int index, int val) { |
| 220 | + List<int[]> a = this.shot.get(index); |
| 221 | + if (a.get(a.size() - 1)[0] == this.snapId) { |
| 222 | + a.get(a.size() - 1)[1] = val; // Update if snapId matches the last entry |
| 223 | + } else { |
| 224 | + a.add(new int[]{this.snapId, val}); // Otherwise add new entry |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + public int snap() { |
| 229 | + this.snapId++; |
| 230 | + return this.snapId - 1; // Return the current snapId, then increment |
| 231 | + } |
| 232 | + |
| 233 | + public int get(int index, int snapId) { |
| 234 | + List<int[]> a = this.shot.get(index); |
| 235 | + int low = 0, high = a.size() - 1; |
| 236 | + |
| 237 | + // Binary search to find the correct snapshot using snapId |
| 238 | + while (low < high) { |
| 239 | + int mid = (low + high + 1) / 2; |
| 240 | + if (a.get(mid)[0] <= snapId) { |
| 241 | + low = mid; |
| 242 | + } else { |
| 243 | + high = mid - 1; |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + return a.get(low)[1]; // Return the value for the found snapshot |
| 248 | + } |
| 249 | + } |
| 250 | + |
173 | 251 | } |
0 commit comments