Wednesday, October 9, 2019

Leetcode 1146: Snapshot Array

https://leetcode.com/problems/snapshot-array/description/


Implement a SnapshotArray that supports the following interface:
  • SnapshotArray(int length) initializes an array-like data structure with the given length.  Initially, each element equals 0.
  • void set(index, val) sets the element at the given index to be equal to val.
  • int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.
  • int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_id

Example 1:
Input: ["SnapshotArray","set","snap","set","get"]
[[3],[0,5],[],[0,6],[0,0]]
Output: [null,null,0,null,5]
Explanation: 
SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3
snapshotArr.set(0,5);  // Set array[0] = 5
snapshotArr.snap();  // Take a snapshot, return snap_id = 0
snapshotArr.set(0,6);
snapshotArr.get(0,0);  // Get the value of array[0] with snap_id = 0, return 5

Constraints:
  • 1 <= length <= 50000
  • At most 50000 calls will be made to setsnap, and get.
  • 0 <= index < length
  • 0 <= snap_id < (the total number of times we call snap())
  • 0 <= val <= 10^9

Notes:

The size of the array can be very large, and the query number can also be very large. Thus, updating the entire array for every snapshot is not a good idea.

Instead, we just need to update the element that is changed. And we can store this information in a sorted order relative to the snap_id. And the snap_id is monotonically increases.

After storing this information, for getting the value at a specific snap_id, we just need to do a binary search at that index, since the snap_id is sorted.

To this question, we need to find the upper boundary of the snap_id first, then -1 if it is not 0. The reason to do the upper boundary searching is that, we did not update the snap_id for every element, and only update the one when it is changed.

See the code below:

  1. class SnapshotArray {
  2. public:
  3.     SnapshotArray(int length) {
  4.         //initialization
  5.         ct = 0;
  6.         for(int i=0; i<length; ++i) {
  7.             ns.push_back(vector<pair<int, int>>{{ct, 0}});
  8.         }
  9.     }
  10.  
  11.     void set(int index, int val) {
  12.         ns[index].push_back({ct, val});
  13.     }
  14.  
  15.     int snap() {
  16.         return ct++;
  17.     }
  18.  
  19.     int get(int index, int snap_id) {
  20.         int ps = help(ns, index, snap_id);
  21.         if(ps>0) --ps;
  22.         return ns[index][ps].second;
  23.     }
  24.  
  25. private:
  26.     vector<vector<pair<int, int>>> ns;
  27.     int ct;
  28.     int help(vector<vector<pair<int, int>>> &ns, int i, int id) {
  29.         int left = 0, right = ns[i].size();
  30.         while(left < right) {
  31.             int mid = left + (right - left) /2;
  32.             if(ns[i][mid].first <= id) left = mid + 1;
  33.             else right = mid;
  34.         }
  35.         return left;
  36.     }
  37. };
  38.  
  39. /**
  40.  * Your SnapshotArray object will be instantiated and called as such:
  41.  * SnapshotArray* obj = new SnapshotArray(length);
  42.  * obj->set(index,val);
  43.  * int param_2 = obj->snap();
  44.  * int param_3 = obj->get(index,snap_id);
  45.  */

No comments:

Post a Comment