Skip to content

Commit 5e7b676

Browse files
authored
Merge pull request #885 from 0xff-dev/900
Add solution and test-cases for problem 900
2 parents 59d2416 + 0ba1e50 commit 5e7b676

File tree

3 files changed

+66
-29
lines changed

3 files changed

+66
-29
lines changed

leetcode/801-900/0900.RLE-Iterator/README.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
# [900.RLE Iterator][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
75

8-
**Example 1:**
6+
- 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`.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
Given a run-length encoded array, design an iterator that iterates through it.
149

15-
## 题意
16-
> ...
10+
Implement the `RLEIterator` class:
1711

18-
## 题解
12+
- `RLEIterator(int[] encoded)` Initializes the object with the encoded array `encoded`.
13+
- `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.
1914

20-
### 思路1
21-
> ...
22-
RLE Iterator
23-
```go
24-
```
15+
**Example 1:**
2516

17+
```
18+
Input
19+
["RLEIterator", "next", "next", "next", "next"]
20+
[[[3, 8, 0, 9, 2, 5]], [2], [1], [1], [2]]
21+
Output
22+
[null, 8, 8, 5, -1]
23+
24+
Explanation
25+
RLEIterator rLEIterator = new RLEIterator([3, 8, 0, 9, 2, 5]); // This maps to the sequence [8,8,8,5,5].
26+
rLEIterator.next(2); // exhausts 2 terms of the sequence, returning 8. The remaining sequence is now [8, 5, 5].
27+
rLEIterator.next(1); // exhausts 1 term of the sequence, returning 8. The remaining sequence is now [5, 5].
28+
rLEIterator.next(1); // exhausts 1 term of the sequence, returning 5. The remaining sequence is now [5].
29+
rLEIterator.next(2); // exhausts 2 terms, returning -1. This is because the first term exhausted was 5,
30+
but the second term did not exist. Since the last term exhausted does not exist, we return -1.
31+
```
2632

2733
## 结语
2834

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type RLEIterator struct {
4+
source []int
5+
index int
6+
}
7+
8+
func Constructor900(encoding []int) RLEIterator {
9+
rle := RLEIterator{source: make([]int, 0), index: 0}
10+
for i := 0; i < len(encoding)-1; i += 2 {
11+
if encoding[i] == 0 {
12+
continue
13+
}
14+
rle.source = append(rle.source, encoding[i], encoding[i+1])
15+
}
16+
return rle
17+
}
18+
19+
func (this *RLEIterator) Next(n int) int {
20+
for ; this.index < len(this.source)-1 && this.source[this.index] < n; this.index += 2 {
21+
n -= this.source[this.index]
22+
this.source[this.index] = 0
23+
}
24+
if this.index == len(this.source) {
25+
return -1
26+
}
27+
this.source[this.index] -= n
28+
return this.source[this.index+1]
29+
}
30+
31+
func Solution(encodings []int, input []int) []int {
32+
c := Constructor900(encodings)
33+
ans := make([]int, len(input))
34+
for i, n := range input {
35+
ans[i] = c.Next(n)
36+
}
37+
return ans
538
}

leetcode/801-900/0900.RLE-Iterator/Solution_test.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,29 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
encodings, input []int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{3, 8, 0, 9, 2, 5}, []int{2, 1, 1, 2}, []int{8, 8, 5, -1}},
1917
}
2018

2119
// 开始测试
2220
for i, c := range cases {
2321
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
22+
got := Solution(c.encodings, c.input)
2523
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
24+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
25+
c.expect, got, c.encodings, c.input)
2826
}
2927
})
3028
}
3129
}
3230

33-
// 压力测试
31+
// 压力测试
3432
func BenchmarkSolution(b *testing.B) {
3533
}
3634

37-
// 使用案列
35+
// 使用案列
3836
func ExampleSolution() {
3937
}

0 commit comments

Comments
 (0)