
Sponsored
Sponsored
This approach involves using a dictionary to map each index to another dictionary, which maps the snapshot version to the value at that index. Each call to set(index, val) updates our dictionary for the current snapshot version. When snap() is called, it simply increments the snapshot counter and returns the previous version of the snapshot. For get(index, snap_id), we check the dictionary at that index for the most recent version less than or equal to snap_id.
Time Complexity: O(1) for set() and snap(); O(n) worst-case for get(), where n is the number of snapshots.
Space Complexity: O(l + m) where l is the length of the array, and m is the number of set operations.
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5typedef struct Node {
6 int snap_id;
7 int value;
8 struct Node *next;
9} Node;
10
11typedef struct {
12 Node **arr;
13 int length;
14 int snap_id;
15} SnapshotArray;
16
17SnapshotArray* snapshotArrayCreate(int length) {
18 SnapshotArray* obj = (SnapshotArray*)malloc(sizeof(SnapshotArray));
19 obj->arr = (Node**)malloc(sizeof(Node*) * length);
20 obj->length = length;
21 obj->snap_id = 0;
22 for (int i = 0; i < length; i++) {
23 obj->arr[i] = (Node*)malloc(sizeof(Node));
24 obj->arr[i]->snap_id = -1;
25 obj->arr[i]->value = 0;
26 obj->arr[i]->next = NULL;
27 }
28 return obj;
29}
30
31void snapshotArraySet(SnapshotArray* obj, int index, int val) {
32 Node* node = (Node*)malloc(sizeof(Node));
33 node->snap_id = obj->snap_id;
34 node->value = val;
35 node->next = obj->arr[index];
36 obj->arr[index] = node;
37}
38
39int snapshotArraySnap(SnapshotArray* obj) {
40 return obj->snap_id++;
41}
42
43int snapshotArrayGet(SnapshotArray* obj, int index, int snap_id) {
44 Node* curr = obj->arr[index];
45 while (curr && curr->snap_id > snap_id) {
46 curr = curr->next;
47 }
48 return (curr) ? curr->value : 0;
49}
50
51void snapshotArrayFree(SnapshotArray* obj) {
52 for (int i = 0; i < obj->length; i++) {
53 Node* curr = obj->arr[i];
54 while (curr) {
55 Node* temp = curr;
56 curr = curr->next;
57 free(temp);
58 }
59 }
60 free(obj->arr);
61 free(obj);
62}Implementing in C involves handling raw pointers and dynamic memory directly. We create a linked list for each index to store its changes across snapshots. The set() function adds a new node for the current snapshot id. The snap() method simply increments the snapshot id. The get() function traverses the list to find the value at the snapshot id or nearest previous snapshot. Memory management is crucial to free allocated nodes.
This approach takes advantage of a more compact representation by only storing changes using a sorted list of tuples for each index. Each tuple contains a snapshot_id and the corresponding value. This method optimizes memory usage by skipping the storage of unaltered initial values, effectively treating them as zeroes until explicitly set.
Time Complexity: O(log n) for get() due to binary search; O(1) for set() and snap().
Space Complexity: O(l + m).
1class SnapshotArray {
2 constructor(
In JavaScript, we utilize an array of lists where each list is sorted by snapshot ids. The set() method either updates the last element of the list if the snapshot ids match or appends a new pair. snap() increments the snapshot id. For get(), binary search finds the largest snapshot id less than or equal to snap_id.