[Leetcode week race 148] Array 1146 Snapshot

1146 Snapshot Array Array Snapshot

description

The implementation supports the following interfaces' snapshot array "- SnapshotArray:

  • SnapshotArray (int length) - initializes a specified length equal to the 类数组data structure. Initially, each element is equal to 0 .
  • void set (index, val) - will be specified index indexelement at the set val.
  • int snap () - take a snapshot of the array, and returns the number of snapshots snap_id(snapshot number is the total number of calls snap () minus 1).
  • int get (index, snap_id) - according to a specified snap_idselection snapshot, and the snapshot to return the specified index indexvalue.

  • Examples

    Input : [ "SnapshotArray", "SET", "SNAP", "SET", "GET"]
    [[. 3], [0,5], [], [0,6], [0,0]]
    Output : [null, null, 0, null, 5]
    explains:
    SnapshotArray snapshotArr = new new SnapshotArray (3); // initialize a snapshot array of length 3
    snapshotArr.set (0,5); // make the array [0] = . 5
    snapshotArr.snap (); // take a snapshot, returns snap_id = 0
    snapshotArr.set (0,6);
    snapshotArr.get (0,0); // get the value of the snapshot snap_id = 0 in the array [0], and returns 5

Thinking

Each location to open up a space that is only saved in this location 变化
because of the number of changes in each location is not the same
when you want value, they look for less input snap_id latest变化

  • Use List + Map data structures to build an array of snapshots

List changes in store for each position
value after the change: TreeMap single storage changes in key: snapshot number value
Use List + Map

  • Examples of its structure

Code

ArrayList + TreeMap

class SnapshotArray {
    // List 存储每个位置的变动情况
    // TreeMap 存储单次变动情况 key:快照编号 value:变动后的值
    // [index,[(snapId, val), (snapId, val)...]]
    List<TreeMap<Integer, Integer>> list = new ArrayList<>();
    // 当前快照编号
    int snapId = 0;

    // 初始化
    public SnapshotArray(int length) {
        snapId = 0;
        // 开始每个位置全为0
        for (int i = 0; i < length; i++) {
            TreeMap<Integer, Integer> t = new TreeMap<>();
            t.put(snapId, 0);
            list.add(t);
        }
    }
    
    public void set(int index, int val) {
        // 重复就覆盖
        list.get(index).put(snapId, val);
    }
    
    public int snap() {
        int res = snapId;
        snapId++;
        return res;
    }
    
    /**
     * 难点 
     * 需要在TreeMap的key值中寻找小于快照编号snap_id的数据
     * 
     * 这里取巧使用了自带的方法 
     * floorEntry(key) 找到key值接近snap_id的实体Entry
     **/
    public int get(int index, int snap_id) {
        TreeMap<Integer, Integer> t = list.get(index);
        return t.floorEntry(snap_id).getValue();
    }
}

/**
 * Your SnapshotArray object will be instantiated and called as such:
 * SnapshotArray obj = new SnapshotArray(length);
 * obj.set(index,val);
 * int param_2 = obj.snap();
 * int param_3 = obj.get(index,snap_id);
 */

Guess you like

Origin www.cnblogs.com/slowbirdoflsh/p/11314537.html