-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
73 lines (68 loc) · 2.29 KB
/
Copy pathsolution.java
File metadata and controls
73 lines (68 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 1146. Snapshot Array
// https://leetcode.com/problems/snapshot-array/
// Medium | Java | Accepted 2026-08-11
// Runtime 52 ms | Memory 129.9 MB
class SnapshotArray {
private class Node {
int snapId;
int val;
Node(int id, int v)
{
snapId = id;
val = v;
}
}
List<Node>[] snapshots; //Contains the nodes of the changes to the array
int snapshot;
public SnapshotArray(int length) {
snapshots = new ArrayList[length]; //Make an array of ArrayLists containing the changes
snapshot = 0;
for(int i = 0; i<length; i++)
{
snapshots[i] = new ArrayList<>();
snapshots[i].add(new Node(0, 0));
}
}
public void set(int index, int val) {
List<Node> list = snapshots[index]; //First get the list of changes for the specific index
Node last = list.get(list.size()-1); //Get the most recent change
if(last.snapId==snapshot) //This checks if the next snapshot is going to be this current Node, meaning we need to update the values of the node
{
last.val = val;
}
else
{
snapshots[index].add(new Node(snapshot, val)); //If the snapshot values are different, this means we called snap and took a snapshot of the current node and need to add a new node for the next snapshot
}
}
public int snap() {
return snapshot++;
}
public int get(int index, int snap_id) {
int l = 0; //Uses binary search to find the snap_id
int r = snapshots[index].size()-1;
int ans = 0;
List<Node> temp = snapshots[index];
while(l<=r)
{
int mid = (l+r)/2;
if(temp.get(mid).snapId>snap_id)
{
r = mid-1;
}
else if(temp.get(mid).snapId<=snap_id)
{
l = mid+1;
ans = mid; //If the snapId at mid is less than or equal to, then that means it could be mid
}
}
return temp.get(ans).val;
}
}
/**
* Your SnapshotArray object will be instantiated and called as such:
* SnapshotArray obj = new SnapshotArray(length);
* obj.set(index,val);
* int param_2 = obj.snap();
* int param_3 = obj.get(index,snap_id);
*/