Skip to content

Instantly share code, notes, and snippets.

@brugnara
Created December 15, 2020 19:17
Show Gist options
  • Save brugnara/124b7b4bb2e31b6aff37f86a14deedbd to your computer and use it in GitHub Desktop.
Save brugnara/124b7b4bb2e31b6aff37f86a14deedbd to your computer and use it in GitHub Desktop.
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