Title: #1146 - Snapshot Array - LeetCode JavaScript Solutions
Open Graph Title: #1146 - Snapshot Array - LeetCode JavaScript Solutions
X Title: #1146 - Snapshot Array - LeetCode JavaScript Solutions
Description: Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializes an array-like data structure with th...
Open Graph Description: Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializes an array-like data structure with th...
X Description: Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializes an array-like data structure with th...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/snapshot-array
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/snapshot-array","name":"#1146 - Snapshot Array - LeetCode JavaScript Solutions","description":"Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializes an array-like data structure with th...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number} length\n */\nvar SnapshotArray = function(length) {\n this.history = new Map();\n this.snapshots = 0;\n this.length = length;\n};\n\n/**\n * @param {number} index\n * @param {number} val\n * @return {void}\n */\nSnapshotArray.prototype.set = function(index, val) {\n const current = this.history.get(this.snapshots) || new Map();\n current.set(index, val);\n this.history.set(this.snapshots, current);\n};\n\n/**\n * @return {number}\n */\nSnapshotArray.prototype.snap = function() {\n return this.snapshots++;\n};\n\n/**\n * @param {number} index\n * @param {number} snapId\n * @return {number}\n */\nSnapshotArray.prototype.get = function(index, snapId) {\n for (let id = snapId; id >= 0; id--) {\n const snapshot = this.history.get(id);\n if (snapshot && snapshot.has(index)) {\n return snapshot.get(index);\n }\n }\n return 0;\n};","keywords":"LeetCode 1146, #1146 - Snapshot Array, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/snapshot-array |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/snapshot-array/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/1146-snapshot-array.js |
| Array | https://leetcodejavascript.com/tags/array |
| Hash Table | https://leetcodejavascript.com/tags/hash-table |
| Binary Search | https://leetcodejavascript.com/tags/binary-search |
| Design | https://leetcodejavascript.com/tags/design |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width,initial-scale=1
Robots: index, follow