diff --git a/leetcode/1101-1200/1146.Snapshot-Array/README.md b/leetcode/1101-1200/1146.Snapshot-Array/README.md index 58b5561d2..51ad22cab 100644 --- a/leetcode/1101-1200/1146.Snapshot-Array/README.md +++ b/leetcode/1101-1200/1146.Snapshot-Array/README.md @@ -1,28 +1,29 @@ # [1146.Snapshot Array][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +Implement a SnapshotArray that supports the following interface: -**Example 1:** +- `SnapshotArray(int length)` initializes an array-like data structure with the given length. **Initially, each element equals 0**. -``` -Input: a = "11", b = "1" -Output: "100" -``` +- `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` -### 思路1 -> ... -Snapshot Array -```go -``` +**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 +``` ## 结语 diff --git a/leetcode/1101-1200/1146.Snapshot-Array/Solution.go b/leetcode/1101-1200/1146.Snapshot-Array/Solution.go index d115ccf5e..322b97b68 100644 --- a/leetcode/1101-1200/1146.Snapshot-Array/Solution.go +++ b/leetcode/1101-1200/1146.Snapshot-Array/Solution.go @@ -1,5 +1,65 @@ package Solution -func Solution(x bool) bool { - return x +type idWithVal struct { + id, val int +} +type SnapshotArray struct { + snpaId int + data [][]idWithVal +} + +func Constructor(length int) SnapshotArray { + data := make([][]idWithVal, length) + for i := 0; i < length; i++ { + data[i] = []idWithVal{{0, 0}} + } + return SnapshotArray{snpaId: 0, data: data} +} + +func (this *SnapshotArray) Set(index int, val int) { + length := len(this.data[index]) + if this.data[index][length-1].id != this.snpaId { + this.data[index] = append(this.data[index], idWithVal{id: this.snpaId, val: val}) + return + } + this.data[index][length-1].val = val +} + +func (this *SnapshotArray) Snap() int { + id := this.snpaId + this.snpaId++ + return id +} + +func (this *SnapshotArray) Get(index int, snap_id int) int { + // 该用二分 + length := len(this.data[index]) + for i := length - 1; i >= 0; i-- { + if this.data[index][i].id <= snap_id { + return this.data[index][i].val + } + } + return 0 +} + +type op struct { + name string + index, val, id int +} + +func Solution(length int, ops []op) []int { + c := Constructor(length) + ans := make([]int, 0) + for _, _op := range ops { + if _op.name == "set" { + c.Set(_op.index, _op.val) + continue + } + if _op.name == "snap" { + ans = append(ans, c.Snap()) + continue + } + ans = append(ans, c.Get(_op.index, _op.id)) + } + return ans } diff --git a/leetcode/1101-1200/1146.Snapshot-Array/Solution_test.go b/leetcode/1101-1200/1146.Snapshot-Array/Solution_test.go index 14ff50eb4..09e23e591 100644 --- a/leetcode/1101-1200/1146.Snapshot-Array/Solution_test.go +++ b/leetcode/1101-1200/1146.Snapshot-Array/Solution_test.go @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + length int + ops []op + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 3, []op{{name: "set", index: 0, val: 5}, {name: "snap"}, {name: "set", index: 0, val: 6}, {name: "get", index: 0, id: 0}}, []int{0, 5}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.length, c.ops) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.length, c.ops) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }