Skip to content

Commit eb28b59

Browse files
add 1146
1 parent 947d4b7 commit eb28b59

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,5 @@ LeetCode
467467
|1139|[Largest 1-Bordered Square](https://leetcode.com/problems/largest-1-bordered-square/) | c | [c++](./src/1139-Largest-1-Bordered-Square/1139.cpp) |[python](./src/1139-Largest-1-Bordered-Square/1139.py)|[go](./src/1139-Largest-1-Bordered-Square/1139.go)|[js](./src/1139-Largest-1-Bordered-Square/1139.js)|Medium|
468468
|1140|[Stone Game II](https://leetcode.com/problems/stone-game-ii/) | c | [c++](./src/1140-Stone-Game-II/1140.cpp) |[python](./src/1140-Stone-Game-II/1140.py)|[go](./src/1140-Stone-Game-II/1140.go)|[js](./src/1140-Stone-Game-II/1140.js)|Medium|
469469
|1144|[Decrease Elements To Make Array Zigzag](https://leetcode.com/problems/decrease-elements-to-make-array-zigzag/) | c | [c++](./src/1144-Decrease-Elements-To-Make-Array-Zigzag/1144.cpp) |[python](./src/1144-Decrease-Elements-To-Make-Array-Zigzag/1144.py)|[go](./src/1144-Decrease-Elements-To-Make-Array-Zigzag/1144.go)|[js](./src/1144-Decrease-Elements-To-Make-Array-Zigzag/1144.js)|Medium|
470-
|1144|[Binary Tree Coloring Game](https://leetcode.com/problems/binary-tree-coloring-game/) | c | [c++](./src/1145-Binary-Tree-Coloring-Game/1145.cpp) |[python](./src/1145-Binary-Tree-Coloring-Game/1145.py)|[go](./src/1145-Binary-Tree-Coloring-Game/1145.go)|[js](./src/1145-Binary-Tree-Coloring-Game/1145.js)|Medium|
470+
|1145|[Binary Tree Coloring Game](https://leetcode.com/problems/binary-tree-coloring-game/) | c | [c++](./src/1145-Binary-Tree-Coloring-Game/1145.cpp) |[python](./src/1145-Binary-Tree-Coloring-Game/1145.py)|[go](./src/1145-Binary-Tree-Coloring-Game/1145.go)|[js](./src/1145-Binary-Tree-Coloring-Game/1145.js)|Medium|
471+
|1146|[Snapshot Array](https://leetcode.com/problems/snapshot-array/) | c | [c++](./src/1146-Snapshot-Array/1146.cpp) |[python](./src/1146-Snapshot-Array/1146.py)|[go](./src/1146-Snapshot-Array/1146.go)|[js](./src/1146-Snapshot-Array/1146.js)|Medium|

src/1146-Snapshot-Array/1146.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class SnapshotArray
2+
{
3+
public:
4+
SnapshotArray(int length) {}
5+
6+
void set(int index, int val)
7+
{
8+
dif[index] = val;
9+
}
10+
11+
int snap()
12+
{
13+
snaps.push_back(dif);
14+
return snaps.size() - 1;
15+
}
16+
17+
int get(int index, int snap_id)
18+
{
19+
if (snaps[snap_id].count(index)) return snaps[snap_id][index];
20+
return 0;
21+
}
22+
private:
23+
unordered_map<int, int> dif;
24+
vector<unordered_map<int, int>> snaps;
25+
};

src/1146-Snapshot-Array/1146.go

Whitespace-only changes.

src/1146-Snapshot-Array/1146.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number} length
3+
*/
4+
var SnapshotArray = function(length) {
5+
this.dif = new Map();
6+
this.snaps = [];
7+
};
8+
9+
/**
10+
* @param {number} index
11+
* @param {number} val
12+
* @return {void}
13+
*/
14+
SnapshotArray.prototype.set = function(index, val) {
15+
this.dif.set(index, val);
16+
};
17+
18+
/**
19+
* @return {number}
20+
*/
21+
SnapshotArray.prototype.snap = function() {
22+
this.snaps.push(new Map(this.dif));
23+
return this.snaps.length - 1;
24+
};
25+
26+
/**
27+
* @param {number} index
28+
* @param {number} snap_id
29+
* @return {number}
30+
*/
31+
SnapshotArray.prototype.get = function(index, snap_id) {
32+
if (this.snaps[snap_id].has(index)) {
33+
return this.snaps[snap_id].get(index);
34+
}
35+
return 0;
36+
};

src/1146-Snapshot-Array/1146.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class SnapshotArray:
2+
def __init__(self, length: int):
3+
self.dif = {}
4+
self.snaps = []
5+
6+
def set(self, index: int, val: int) -> None:
7+
self.dif[index] = val
8+
9+
def snap(self) -> int:
10+
self.snaps.append(self.dif.copy())
11+
return len(self.snaps) - 1
12+
13+
def get(self, index: int, snap_id: int) -> int:
14+
return self.snaps[snap_id][index] if index in self.snaps[snap_id] else 0

0 commit comments

Comments
 (0)