-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnapshot-array.rs
64 lines (54 loc) · 1.58 KB
/
snapshot-array.rs
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
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
/**
* Your SnapshotArray object will be instantiated and called as such:
* let obj = SnapshotArray::new(length);
* obj.set(index, val);
* let ret_2: i32 = obj.snap();
* let ret_3: i32 = obj.get(index, snap_id);
*/
struct SnapshotArray {
snapshot_id: std::cell::Cell<i32>,
storage: Vec<Vec<(i32, i32)>>,
}
impl SnapshotArray {
fn new(length: i32) -> Self {
Self {
snapshot_id: std::cell::Cell::new(0),
storage: {
let mut data = Vec::with_capacity(length as usize);
for _ in 0..length {
data.push(vec![]);
}
data
},
}
}
fn set(&mut self, index: i32, val: i32) {
if let Some(x) = self.storage[index as usize].last_mut() {
if x.0 == self.snapshot_id.get() {
x.1 = val;
return;
}
}
self.storage[index as usize].push((self.snapshot_id.get(), val));
}
fn snap(&self) -> i32 {
self.snapshot_id.set(self.snapshot_id.get() + 1);
self.snapshot_id.get() - 1
}
fn get(&self, index: i32, snap_id: i32) -> i32 {
let s = self.storage.get(index as usize).unwrap();
let i = s.partition_point(|x| x.0 <= snap_id);
if i > 0 {
s[i - 1].1
} else {
0
}
}
}