Skip to content

Commit a43b08a

Browse files
committed
feat: add solutions to lc problem: No.1146
No.1146.Snapshot Array
1 parent d80c1cb commit a43b08a

File tree

6 files changed

+446
-0
lines changed

6 files changed

+446
-0
lines changed

solution/1100-1199/1146.Snapshot Array/README.md

+158
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,180 @@ snapshotArr.get(0,0); // 获取 snap_id = 0 的快照中 array[0] 的值,返
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:数组 + 二分查找**
49+
50+
维护一个数组,数组中的每个元素是一个列表,列表中存储的是每次设置的值以及对应的快照编号。
51+
52+
每次设置值时,将值和快照编号添加到对应索引的列表中。
53+
54+
每次获取值时,使用二分查找,找到对应位置第一个大于快照编号 `snap_id` 的值,然后返回前一个值即可。
55+
56+
时间复杂度上,设置值的时间复杂度为 $O(1)$,快照的时间复杂度为 $O(1)$,获取值的时间复杂度为 $O(\log n)$。
57+
4858
<!-- tabs:start -->
4959

5060
### **Python3**
5161

5262
<!-- 这里可写当前语言的特殊实现逻辑 -->
5363

5464
```python
65+
class SnapshotArray:
66+
67+
def __init__(self, length: int):
68+
self.idx = 0
69+
self.arr = defaultdict(list)
70+
71+
def set(self, index: int, val: int) -> None:
72+
self.arr[index].append((self.idx, val))
5573

74+
def snap(self) -> int:
75+
self.idx += 1
76+
return self.idx - 1
77+
78+
def get(self, index: int, snap_id: int) -> int:
79+
vals = self.arr[index]
80+
i = bisect_right(vals, (snap_id, inf)) - 1
81+
return 0 if i < 0 else vals[i][1]
82+
83+
84+
# Your SnapshotArray object will be instantiated and called as such:
85+
# obj = SnapshotArray(length)
86+
# obj.set(index,val)
87+
# param_2 = obj.snap()
88+
# param_3 = obj.get(index,snap_id)
5689
```
5790

5891
### **Java**
5992

6093
<!-- 这里可写当前语言的特殊实现逻辑 -->
6194

6295
```java
96+
class SnapshotArray {
97+
private List<int[]>[] arr;
98+
private int idx;
99+
100+
public SnapshotArray(int length) {
101+
arr = new List[length];
102+
Arrays.setAll(arr, k -> new ArrayList<>());
103+
}
104+
105+
public void set(int index, int val) {
106+
arr[index].add(new int[] {idx, val});
107+
}
108+
109+
public int snap() {
110+
return idx++;
111+
}
112+
113+
public int get(int index, int snap_id) {
114+
var vals = arr[index];
115+
int left = 0, right = vals.size();
116+
while (left < right) {
117+
int mid = (left + right) >> 1;
118+
if (vals.get(mid)[0] > snap_id) {
119+
right = mid;
120+
} else {
121+
left = mid + 1;
122+
}
123+
}
124+
return left == 0 ? 0 : vals.get(left - 1)[1];
125+
}
126+
}
127+
128+
/**
129+
* Your SnapshotArray object will be instantiated and called as such:
130+
* SnapshotArray obj = new SnapshotArray(length);
131+
* obj.set(index,val);
132+
* int param_2 = obj.snap();
133+
* int param_3 = obj.get(index,snap_id);
134+
*/
135+
```
136+
137+
### **C++**
138+
139+
```cpp
140+
class SnapshotArray {
141+
public:
142+
SnapshotArray(int length) {
143+
idx = 0;
144+
arr = vector<vector<pair<int, int>>>(length);
145+
}
146+
147+
void set(int index, int val) {
148+
arr[index].push_back({idx, val});
149+
}
150+
151+
int snap() {
152+
return idx++;
153+
}
154+
155+
int get(int index, int snap_id) {
156+
auto& vals = arr[index];
157+
int left = 0, right = vals.size();
158+
while (left < right) {
159+
int mid = (left + right) >> 1;
160+
if (vals[mid].first > snap_id) {
161+
right = mid;
162+
} else {
163+
left = mid + 1;
164+
}
165+
}
166+
return left == 0 ? 0 : vals[left - 1].second;
167+
}
168+
169+
private:
170+
vector<vector<pair<int, int>>> arr;
171+
int idx;
172+
};
173+
174+
/**
175+
* Your SnapshotArray object will be instantiated and called as such:
176+
* SnapshotArray* obj = new SnapshotArray(length);
177+
* obj->set(index,val);
178+
* int param_2 = obj->snap();
179+
* int param_3 = obj->get(index,snap_id);
180+
*/
181+
```
63182

183+
### **Go**
184+
185+
```go
186+
type SnapshotArray struct {
187+
idx int
188+
arr [][]pair
189+
}
190+
191+
func Constructor(length int) SnapshotArray {
192+
return SnapshotArray{0, make([][]pair, length)}
193+
}
194+
195+
func (this *SnapshotArray) Set(index int, val int) {
196+
this.arr[index] = append(this.arr[index], pair{this.idx, val})
197+
}
198+
199+
func (this *SnapshotArray) Snap() int {
200+
this.idx++
201+
return this.idx - 1
202+
}
203+
204+
func (this *SnapshotArray) Get(index int, snap_id int) int {
205+
vals := this.arr[index]
206+
i := sort.Search(len(vals), func(i int) bool { return vals[i].i > snap_id })
207+
if i == 0 {
208+
return 0
209+
}
210+
return vals[i-1].v
211+
}
212+
213+
type pair struct{ i, v int }
214+
215+
/**
216+
* Your SnapshotArray object will be instantiated and called as such:
217+
* obj := Constructor(length);
218+
* obj.Set(index,val);
219+
* param_2 := obj.Snap();
220+
* param_3 := obj.Get(index,snap_id);
221+
*/
64222
```
65223

66224
### **...**

solution/1100-1199/1146.Snapshot Array/README_EN.md

+148
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,161 @@ snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5</
4545
### **Python3**
4646

4747
```python
48+
class SnapshotArray:
4849

50+
def __init__(self, length: int):
51+
self.idx = 0
52+
self.arr = defaultdict(list)
53+
54+
def set(self, index: int, val: int) -> None:
55+
self.arr[index].append((self.idx, val))
56+
57+
def snap(self) -> int:
58+
self.idx += 1
59+
return self.idx - 1
60+
61+
def get(self, index: int, snap_id: int) -> int:
62+
vals = self.arr[index]
63+
i = bisect_right(vals, (snap_id, inf)) - 1
64+
return 0 if i < 0 else vals[i][1]
65+
66+
67+
# Your SnapshotArray object will be instantiated and called as such:
68+
# obj = SnapshotArray(length)
69+
# obj.set(index,val)
70+
# param_2 = obj.snap()
71+
# param_3 = obj.get(index,snap_id)
4972
```
5073

5174
### **Java**
5275

5376
```java
77+
class SnapshotArray {
78+
private List<int[]>[] arr;
79+
private int idx;
80+
81+
public SnapshotArray(int length) {
82+
arr = new List[length];
83+
Arrays.setAll(arr, k -> new ArrayList<>());
84+
}
85+
86+
public void set(int index, int val) {
87+
arr[index].add(new int[] {idx, val});
88+
}
89+
90+
public int snap() {
91+
return idx++;
92+
}
93+
94+
public int get(int index, int snap_id) {
95+
var vals = arr[index];
96+
int left = 0, right = vals.size();
97+
while (left < right) {
98+
int mid = (left + right) >> 1;
99+
if (vals.get(mid)[0] > snap_id) {
100+
right = mid;
101+
} else {
102+
left = mid + 1;
103+
}
104+
}
105+
return left == 0 ? 0 : vals.get(left - 1)[1];
106+
}
107+
}
108+
109+
/**
110+
* Your SnapshotArray object will be instantiated and called as such:
111+
* SnapshotArray obj = new SnapshotArray(length);
112+
* obj.set(index,val);
113+
* int param_2 = obj.snap();
114+
* int param_3 = obj.get(index,snap_id);
115+
*/
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class SnapshotArray {
122+
public:
123+
SnapshotArray(int length) {
124+
idx = 0;
125+
arr = vector<vector<pair<int, int>>>(length);
126+
}
127+
128+
void set(int index, int val) {
129+
arr[index].push_back({idx, val});
130+
}
131+
132+
int snap() {
133+
return idx++;
134+
}
135+
136+
int get(int index, int snap_id) {
137+
auto& vals = arr[index];
138+
int left = 0, right = vals.size();
139+
while (left < right) {
140+
int mid = (left + right) >> 1;
141+
if (vals[mid].first > snap_id) {
142+
right = mid;
143+
} else {
144+
left = mid + 1;
145+
}
146+
}
147+
return left == 0 ? 0 : vals[left - 1].second;
148+
}
149+
150+
private:
151+
vector<vector<pair<int, int>>> arr;
152+
int idx;
153+
};
154+
155+
/**
156+
* Your SnapshotArray object will be instantiated and called as such:
157+
* SnapshotArray* obj = new SnapshotArray(length);
158+
* obj->set(index,val);
159+
* int param_2 = obj->snap();
160+
* int param_3 = obj->get(index,snap_id);
161+
*/
162+
```
54163
164+
### **Go**
165+
166+
```go
167+
type SnapshotArray struct {
168+
idx int
169+
arr [][]pair
170+
}
171+
172+
func Constructor(length int) SnapshotArray {
173+
return SnapshotArray{0, make([][]pair, length)}
174+
}
175+
176+
func (this *SnapshotArray) Set(index int, val int) {
177+
this.arr[index] = append(this.arr[index], pair{this.idx, val})
178+
}
179+
180+
func (this *SnapshotArray) Snap() int {
181+
this.idx++
182+
return this.idx - 1
183+
}
184+
185+
func (this *SnapshotArray) Get(index int, snap_id int) int {
186+
vals := this.arr[index]
187+
i := sort.Search(len(vals), func(i int) bool { return vals[i].i > snap_id })
188+
if i == 0 {
189+
return 0
190+
}
191+
return vals[i-1].v
192+
}
193+
194+
type pair struct{ i, v int }
195+
196+
/**
197+
* Your SnapshotArray object will be instantiated and called as such:
198+
* obj := Constructor(length);
199+
* obj.Set(index,val);
200+
* param_2 := obj.Snap();
201+
* param_3 := obj.Get(index,snap_id);
202+
*/
55203
```
56204

57205
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class SnapshotArray {
2+
public:
3+
SnapshotArray(int length) {
4+
idx = 0;
5+
arr = vector<vector<pair<int, int>>>(length);
6+
}
7+
8+
void set(int index, int val) {
9+
arr[index].push_back({idx, val});
10+
}
11+
12+
int snap() {
13+
return idx++;
14+
}
15+
16+
int get(int index, int snap_id) {
17+
auto& vals = arr[index];
18+
int left = 0, right = vals.size();
19+
while (left < right) {
20+
int mid = (left + right) >> 1;
21+
if (vals[mid].first > snap_id) {
22+
right = mid;
23+
} else {
24+
left = mid + 1;
25+
}
26+
}
27+
return left == 0 ? 0 : vals[left - 1].second;
28+
}
29+
30+
private:
31+
vector<vector<pair<int, int>>> arr;
32+
int idx;
33+
};
34+
35+
/**
36+
* Your SnapshotArray object will be instantiated and called as such:
37+
* SnapshotArray* obj = new SnapshotArray(length);
38+
* obj->set(index,val);
39+
* int param_2 = obj->snap();
40+
* int param_3 = obj->get(index,snap_id);
41+
*/

0 commit comments

Comments
 (0)