1146. Snapshot Array Medium

@problem@discussion
#Array#Hash Table#Binary Search#Design



1/**
2 * [1146] Snapshot Array
3 *
4 * Implement a SnapshotArray that supports the following interface:
5 * 
6 * 	SnapshotArray(int length) initializes an array-like data structure with the given length. Initially, each element equals 0.
7 * 	void set(index, val) sets the element at the given index to be equal to val.
8 * 	int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.
9 * 	int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_id
10 * 
11 *  
12 * Example 1:
13 * 
14 * Input: ["SnapshotArray","set","snap","set","get"]
15 * [[3],[0,5],[],[0,6],[0,0]]
16 * Output: [null,null,0,null,5]
17 * Explanation: 
18 * SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3
19 * snapshotArr.set(0,5);  // Set array[0] = 5
20 * snapshotArr.snap();  // Take a snapshot, return snap_id = 0
21 * snapshotArr.set(0,6);
22 * snapshotArr.get(0,0);  // Get the value of array[0] with snap_id = 0, return 5
23 *  
24 * Constraints:
25 * 
26 * 	1 <= length <= 5 * 10^4
27 * 	0 <= index < length
28 * 	0 <= val <= 10^9
29 * 	0 <= snap_id < (the total number of times we call snap())
30 * 	At most 5 * 10^4 calls will be made to set, snap, and get.
31 * 
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/snapshot-array/
36// discuss: https://leetcode.com/problems/snapshot-array/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40struct SnapshotArray {
41        false
42    }
43
44
45/** 
46 * `&self` means the method takes an immutable reference.
47 * If you need a mutable reference, change it to `&mut self` instead.
48 */
49impl SnapshotArray {
50
51    fn new(length: i32) -> Self {
52        
53    }
54    
55    fn set(&self, index: i32, val: i32) {
56        
57    }
58    
59    fn snap(&self) -> i32 {
60        
61    }
62    
63    fn get(&self, index: i32, snap_id: i32) -> i32 {
64        
65    }
66}
67
68/**
69 * Your SnapshotArray object will be instantiated and called as such:
70 * let obj = SnapshotArray::new(length);
71 * obj.set(index, val);
72 * let ret_2: i32 = obj.snap();
73 * let ret_3: i32 = obj.get(index, snap_id);
74 */
75
76// submission codes end
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn test_1146() {
84    }
85}
86


Back
© 2025 bowen.ge All Rights Reserved.