Skip to content

Commit 3c78455

Browse files
committedApr 4, 2025
Add solution #1146
1 parent bbd9745 commit 3c78455

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,152 LeetCode solutions in JavaScript
1+
# 1,153 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -900,6 +900,7 @@
900900
1143|[Longest Common Subsequence](./solutions/1143-longest-common-subsequence.js)|Medium|
901901
1144|[Decrease Elements To Make Array Zigzag](./solutions/1144-decrease-elements-to-make-array-zigzag.js)|Medium|
902902
1145|[Binary Tree Coloring Game](./solutions/1145-binary-tree-coloring-game.js)|Medium|
903+
1146|[Snapshot Array](./solutions/1146-snapshot-array.js)|Medium|
903904
1161|[Maximum Level Sum of a Binary Tree](./solutions/1161-maximum-level-sum-of-a-binary-tree.js)|Medium|
904905
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
905906
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|

‎solutions/1146-snapshot-array.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* 1146. Snapshot Array
3+
* https://leetcode.com/problems/snapshot-array/
4+
* Difficulty: Medium
5+
*
6+
* Implement a SnapshotArray that supports the following interface:
7+
* - SnapshotArray(int length) initializes an array-like data structure with the given length.
8+
* Initially, each element equals 0.
9+
* - void set(index, val) sets the element at the given index to be equal to val.
10+
* - int snap() takes a snapshot of the array and returns the snapId: the total number of times
11+
* we called snap() minus 1.
12+
* - int get(index, snapId) returns the value at the given index, at the time we took the
13+
* snapshot with the given snapId
14+
*/
15+
16+
/**
17+
* @param {number} length
18+
*/
19+
var SnapshotArray = function(length) {
20+
this.history = new Map();
21+
this.snapshots = 0;
22+
this.length = length;
23+
};
24+
25+
/**
26+
* @param {number} index
27+
* @param {number} val
28+
* @return {void}
29+
*/
30+
SnapshotArray.prototype.set = function(index, val) {
31+
const current = this.history.get(this.snapshots) || new Map();
32+
current.set(index, val);
33+
this.history.set(this.snapshots, current);
34+
};
35+
36+
/**
37+
* @return {number}
38+
*/
39+
SnapshotArray.prototype.snap = function() {
40+
return this.snapshots++;
41+
};
42+
43+
/**
44+
* @param {number} index
45+
* @param {number} snapId
46+
* @return {number}
47+
*/
48+
SnapshotArray.prototype.get = function(index, snapId) {
49+
for (let id = snapId; id >= 0; id--) {
50+
const snapshot = this.history.get(id);
51+
if (snapshot && snapshot.has(index)) {
52+
return snapshot.get(index);
53+
}
54+
}
55+
return 0;
56+
};

0 commit comments

Comments
 (0)
Please sign in to comment.