zoukankan      html  css  js  c++  java
  • LeetCode 1146. Snapshot Array

    原题链接在这里:https://leetcode.com/problems/snapshot-array/

    题目:

    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

    题解:

    Instead of make a copy of each snapshot, which takes a lot of memory space, we could record the state of cell when calling set method.

    Have a TreeMap array, each TreeMap maintains the states of a cell.

    When calling set, mark current snapshot id with the new value of this cell.

    When calling get, try to get the floor entry with given snapshot id.

    Time Complexity: SnapshotArray, O(length). set, O(logn). snap, O(1). get, O(logn). n is the number of total entries in arr, the number of previoius set call.

    Space: O(n). 

    AC Java:

     1 class SnapshotArray {
     2     TreeMap<Integer, Integer> [] arr;
     3     int snapId;
     4     
     5     public SnapshotArray(int length) {
     6         arr = new TreeMap[length];
     7         for(int i = 0; i<length; i++){
     8             arr[i] = new TreeMap<Integer, Integer>();
     9             arr[i].put(0, 0);
    10         }
    11         
    12         snapId = 0;
    13     }
    14     
    15     public void set(int index, int val) {
    16         arr[index].put(snapId, val);
    17     }
    18     
    19     public int snap() {
    20         return snapId++;
    21     }
    22     
    23     public int get(int index, int snap_id) {
    24         return arr[index].floorEntry(snap_id).getValue();
    25     }
    26 }
    27 
    28 /**
    29  * Your SnapshotArray object will be instantiated and called as such:
    30  * SnapshotArray obj = new SnapshotArray(length);
    31  * obj.set(index,val);
    32  * int param_2 = obj.snap();
    33  * int param_3 = obj.get(index,snap_id);
    34  */
  • 相关阅读:
    考研竞赛数学微信交流群
    华东师范大学数学分析第5版提要03.04.01证明
    大学生数学竞赛题目分类30章之第02章函数极限202104版已发54题
    研赛02994北京大学数学分析习题集2.3.1(3)参考解答
    复旦大学高等代数问题2016A12参考解答
    华东师范大学数学分析第5版03.03函数极限存在的条件
    大学生数学竞赛题目分类30章之第01章数列极限202104版已发33题
    研赛02992北京大学数学分析习题集23.1
    裴礼文数学分析中的典型问题与方法第3版勘误32个
    Evans277-281
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12041830.html
Copyright © 2011-2022 走看看