{"id":5388,"date":"2019-08-04T10:52:50","date_gmt":"2019-08-04T17:52:50","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5388"},"modified":"2019-08-04T11:00:28","modified_gmt":"2019-08-04T18:00:28","slug":"leetcode-1146-snapshot-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/data-structure\/leetcode-1146-snapshot-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1146. Snapshot Array"},"content":{"rendered":"\n<p>Implement a SnapshotArray that supports the following interface:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>SnapshotArray(int length)<\/code>&nbsp;initializes an array-like data structure with the given length.&nbsp;&nbsp;<strong>Initially, each element equals 0<\/strong>.<\/li><li><code>void set(index, val)<\/code>&nbsp;sets the element at the given&nbsp;<code>index<\/code>&nbsp;to be equal to&nbsp;<code>val<\/code>.<\/li><li><code>int snap()<\/code>&nbsp;takes a snapshot of the array and returns the&nbsp;<code>snap_id<\/code>: the total number of times we called&nbsp;<code>snap()<\/code>&nbsp;minus&nbsp;<code>1<\/code>.<\/li><li><code>int get(index, snap_id)<\/code>&nbsp;returns the value at the given&nbsp;<code>index<\/code>, at the time we took the snapshot with the given&nbsp;<code>snap_id<\/code><\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [\"SnapshotArray\",\"set\",\"snap\",\"set\",\"get\"]\n[[3],[0,5],[],[0,6],[0,0]]\n<strong>Output:<\/strong> [null,null,0,null,5]\n<strong>Explanation: <\/strong>\nSnapshotArray snapshotArr = new SnapshotArray(3); \/\/ set the length to be 3\nsnapshotArr.set(0,5);  \/\/ Set array[0] = 5\nsnapshotArr.snap();  \/\/ Take a snapshot, return snap_id = 0\nsnapshotArr.set(0,6);\nsnapshotArr.get(0,0);  \/\/ Get the value of array[0] with snap_id = 0, return 5<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= length&nbsp;&lt;= 50000<\/code><\/li><li>At most&nbsp;<code>50000<\/code>&nbsp;calls will be made to&nbsp;<code>set<\/code>,&nbsp;<code>snap<\/code>, and&nbsp;<code>get<\/code>.<\/li><li><code>0 &lt;= index&nbsp;&lt;&nbsp;length<\/code><\/li><li><code>0 &lt;=&nbsp;snap_id &lt;&nbsp;<\/code>(the total number of times we call&nbsp;<code>snap()<\/code>)<\/li><li><code>0 &lt;=&nbsp;val &lt;= 10^9<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: map + upper_bound<\/strong><\/h2>\n\n\n\n<p>Use a vector to store maps, one map per element.<br>The map stores {snap_id -> val}, use upper_bound to find the first version > snap_id and use previous version&#8217;s value.<\/p>\n\n\n\n<p>Time complexity: <br>Set: O(log|snap_id|)<br>Get: O(log|snap_id|)  <br>Snap: O(1)<br>Space complexity: O(length + set_calls)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass SnapshotArray {\npublic:\n  SnapshotArray(int length): id_(0), vals_(length) {}\n\n  void set(int index, int val) {\n    vals_[index][id_] = val;\n  }\n\n  int snap() { return id_++; }\n\n  int get(int index, int snap_id) const {\n    auto it = vals_[index].upper_bound(snap_id);\n    if (it == begin(vals_[index])) return 0;\n    return prev(it)->second;\n  }\nprivate:\n  int id_;\n  vector<map<int, int>> vals_;\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Implement a SnapshotArray that supports the following interface: SnapshotArray(int length)&nbsp;initializes an array-like data structure with the given length.&nbsp;&nbsp;Initially, each element equals 0. void set(index, val)&nbsp;sets&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89],"tags":[251,462],"class_list":["post-5388","post","type-post","status-publish","format-standard","hentry","category-data-structure","tag-design","tag-map","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5388"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5388\/revisions"}],"predecessor-version":[{"id":5392,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5388\/revisions\/5392"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}