diff --git a/leetcode/801-900/0900.RLE-Iterator/README.md b/leetcode/801-900/0900.RLE-Iterator/README.md index c9f5b2254..8a700dc59 100644 --- a/leetcode/801-900/0900.RLE-Iterator/README.md +++ b/leetcode/801-900/0900.RLE-Iterator/README.md @@ -1,28 +1,34 @@ # [900.RLE Iterator][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 +We can use run-length encoding (i.e., **RLE**) to encode a sequence of integers. In a run-length encoded array of even length `encoding` (**0-indexed**), for all even `i`, `encoding[i]` tells us the number of times that the non-negative integer value `encoding[i + 1]` is repeated in the sequence. -**Example 1:** +- For example, the sequence `arr = [8,8,8,5,5]` can be encoded to be `encoding = [3,8,2,5]`. `encoding = [3,8,0,9,2,5]` and `encoding = [2,8,1,8,2,5]` are also valid **RLE** of `arr`. -``` -Input: a = "11", b = "1" -Output: "100" -``` +Given a run-length encoded array, design an iterator that iterates through it. -## 题意 -> ... +Implement the `RLEIterator` class: -## 题解 +- `RLEIterator(int[] encoded)` Initializes the object with the encoded array `encoded`. +- `int next(int n)` Exhausts the next `n` elements and returns the last element exhausted in this way. If there is no element left to exhaust, return `-1` instead. -### 思路1 -> ... -RLE Iterator -```go -``` +**Example 1:** +``` +Input +["RLEIterator", "next", "next", "next", "next"] +[[[3, 8, 0, 9, 2, 5]], [2], [1], [1], [2]] +Output +[null, 8, 8, 5, -1] + +Explanation +RLEIterator rLEIterator = new RLEIterator([3, 8, 0, 9, 2, 5]); // This maps to the sequence [8,8,8,5,5]. +rLEIterator.next(2); // exhausts 2 terms of the sequence, returning 8. The remaining sequence is now [8, 5, 5]. +rLEIterator.next(1); // exhausts 1 term of the sequence, returning 8. The remaining sequence is now [5, 5]. +rLEIterator.next(1); // exhausts 1 term of the sequence, returning 5. The remaining sequence is now [5]. +rLEIterator.next(2); // exhausts 2 terms, returning -1. This is because the first term exhausted was 5, +but the second term did not exist. Since the last term exhausted does not exist, we return -1. +``` ## 结语 diff --git a/leetcode/801-900/0900.RLE-Iterator/Solution.go b/leetcode/801-900/0900.RLE-Iterator/Solution.go index d115ccf5e..50cfc8dfd 100644 --- a/leetcode/801-900/0900.RLE-Iterator/Solution.go +++ b/leetcode/801-900/0900.RLE-Iterator/Solution.go @@ -1,5 +1,38 @@ package Solution -func Solution(x bool) bool { - return x +type RLEIterator struct { + source []int + index int +} + +func Constructor900(encoding []int) RLEIterator { + rle := RLEIterator{source: make([]int, 0), index: 0} + for i := 0; i < len(encoding)-1; i += 2 { + if encoding[i] == 0 { + continue + } + rle.source = append(rle.source, encoding[i], encoding[i+1]) + } + return rle +} + +func (this *RLEIterator) Next(n int) int { + for ; this.index < len(this.source)-1 && this.source[this.index] < n; this.index += 2 { + n -= this.source[this.index] + this.source[this.index] = 0 + } + if this.index == len(this.source) { + return -1 + } + this.source[this.index] -= n + return this.source[this.index+1] +} + +func Solution(encodings []int, input []int) []int { + c := Constructor900(encodings) + ans := make([]int, len(input)) + for i, n := range input { + ans[i] = c.Next(n) + } + return ans } diff --git a/leetcode/801-900/0900.RLE-Iterator/Solution_test.go b/leetcode/801-900/0900.RLE-Iterator/Solution_test.go index 14ff50eb4..cb5ee5cc5 100644 --- a/leetcode/801-900/0900.RLE-Iterator/Solution_test.go +++ b/leetcode/801-900/0900.RLE-Iterator/Solution_test.go @@ -9,31 +9,29 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + encodings, input []int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{3, 8, 0, 9, 2, 5}, []int{2, 1, 1, 2}, []int{8, 8, 5, -1}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.encodings, c.input) 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.encodings, c.input) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }