Created
December 15, 2020 19:17
-
-
Save brugnara/124b7b4bb2e31b6aff37f86a14deedbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type SnapshotArray struct { | |
hash map[int]map[int]int | |
snap int | |
} | |
func Constructor(length int) SnapshotArray { | |
array := SnapshotArray{ | |
map[int]map[int]int{}, | |
0, | |
} | |
for i:=0;i<length;i++ { | |
array.hash[i] = map[int]int{} | |
} | |
return array | |
} | |
func (this *SnapshotArray) Set(index int, val int) { | |
this.hash[index][this.snap] = val | |
} | |
func (this *SnapshotArray) Snap() (snap int) { | |
// fmt.Println("snap, current:", this.snap) | |
snap = this.snap | |
this.snap++ | |
return | |
} | |
func (this *SnapshotArray) Get(index int, snap_id int) (ret int) { | |
id := snap_id | |
for id >= 0{ | |
_, ok := this.hash[index][id] | |
if ok { | |
break | |
} | |
id-- | |
} | |
if id < 0 { | |
return 0 | |
} | |
ret = this.hash[index][id] | |
// fmt.Println("Getting index:", index, "from snap_id:", snap_id, "ret:", ret ) | |
return | |
} | |
/** | |
* Your SnapshotArray object will be instantiated and called as such: | |
* obj := Constructor(length); | |
* obj.Set(index,val); | |
* param_2 := obj.Snap(); | |
* param_3 := obj.Get(index,snap_id); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment