Skip to content

Commit

Permalink
Add solution and test-cases for problem 900
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed May 24, 2024
1 parent 09a1dcb commit 0ba1e50
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 29 deletions.
38 changes: 22 additions & 16 deletions leetcode/801-900/0900.RLE-Iterator/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
37 changes: 35 additions & 2 deletions leetcode/801-900/0900.RLE-Iterator/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 9 additions & 11 deletions leetcode/801-900/0900.RLE-Iterator/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

0 comments on commit 0ba1e50

Please sign in to comment.