Skip to content

feat: add solutions to lc problems: No.0899~0908 #2108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions solution/0800-0899/0899.Orderly Queue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@

对于任何字符串,如果可以交换任意相邻字符,则可以对字符串中的字符做类似冒泡排序的操作,最终得到一个升序排列的字符串。

**方法一:分类判断**
**方法一:分情况判断**

若 $k=1$,我们每次只能将字符串首字符移动到字符串末尾,总共有 $s.length$ 种不同的状态,我们返回其中字典序最小的字符串即可。
若 $k = 1$,我们每次只能将字符串首字符移动到字符串末尾,总共有 $|s|$ 种不同的状态,我们返回其中字典序最小的字符串即可。

若 $k\gt1$,对于形如 $abc[xy]def$ 的字符串,可以依次将 $a$, $b$, $c$ 移动到最后,得到 $[xy]defabc$,然后将 $y$, $x$ 移动到最后,得到 $defabc[yx]$,最后将 $d$, $e$, $f$ 移动到最后,得到 $abc[yx]def$,这样就实现了对 $y$, $x$ 的交换。
若 $k \gt 1$,对于形如 $abc[xy]def$ 的字符串,可以依次将 $a$, $b$, $c$ 移动到最后,得到 $[xy]defabc$,然后将 $y$, $x$ 移动到最后,得到 $defabc[yx]$,最后将 $d$, $e$, $f$ 移动到最后,得到 $abc[yx]def$,这样就实现了对 $y$, $x$ 的交换。

因此,只要 $k\gt1$,我们就能够交换字符串中的任何两个相邻字符,最终得到一个升序排列的字符串。
因此,只要 $k \gt 1$,我们就能够交换字符串中的任何两个相邻字符,最终得到一个升序排列的字符串。

时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是字符串的长度。

<!-- tabs:start -->

Expand Down
14 changes: 14 additions & 0 deletions solution/0800-0899/0899.Orderly Queue/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ In the second move, we move the 3<sup>rd</sup> character &#39;c&#39; to the end,

## Solutions

**Preface**

For any string, if any adjacent characters can be swapped, we can perform a bubble sort-like operation on the characters in the string, eventually obtaining a string sorted in ascending order.

**Solution 1: Case-by-case Judgment**

If $k = 1$, we can only move the first character of the string to the end of the string each time, resulting in $|s|$ different states. We return the string with the smallest lexicographic order.

If $k > 1$, for a string like $abc[xy]def$, we can move $a$, $b$, and $c$ to the end in order, resulting in $[xy]defabc$. Then we move $y$ and $x$ to the end, resulting in $defabc[yx]$. Finally, we move $d$, $e$, and $f$ to the end, resulting in $abc[yx]def$. This way, we have swapped $y$ and $x$.

Therefore, as long as $k > 1$, we can swap any two adjacent characters in the string, eventually obtaining a string sorted in ascending order.

The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.

<!-- tabs:start -->

### **Python3**
Expand Down
100 changes: 71 additions & 29 deletions solution/0900-0999/0900.RLE Iterator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ rLEIterator.next(2); // 耗去序列的 2 个项,返回 -1。 这是由于第

<!-- 这里可写通用的实现逻辑 -->

**方法一:维护两个指针**

我们定义两个指针 $i$ 和 $j$,其中指针 $i$ 指向当前读取的游程编码,指针 $j$ 指向当前读取的游程编码中的第几个字符。初始时 $i = 0$, $j = 0$。

每次调用 `next(n)` 时,我们判断当前游程编码中剩余的字符数 $encoding[i] - j$ 是否小于 $n$,若是,则将 $n$ 减去 $encoding[i] - j$,并将 $i$ 加 $2$,$j$ 置为 $0$,然后继续判断下一个游程编码;若不是,则将 $j$ 加 $n$,并返回 $encoding[i + 1]$。

若 $i$ 超出了游程编码的长度,依然没有返回值,则说明没有剩余的元素要耗尽,返回 $-1$。

时间复杂度 $O(n + q)$,空间复杂度 $O(n)$。其中 $n$ 是游程编码的长度,而 $q$ 是调用 `next(n)` 的次数。

<!-- tabs:start -->

### **Python3**
Expand All @@ -67,16 +77,16 @@ class RLEIterator:
def __init__(self, encoding: List[int]):
self.encoding = encoding
self.i = 0
self.curr = 0
self.j = 0

def next(self, n: int) -> int:
while self.i < len(self.encoding):
if self.curr + n > self.encoding[self.i]:
n -= self.encoding[self.i] - self.curr
self.curr = 0
if self.encoding[self.i] - self.j < n:
n -= self.encoding[self.i] - self.j
self.i += 2
self.j = 0
else:
self.curr += n
self.j += n
return self.encoding[self.i + 1]
return -1

Expand All @@ -93,23 +103,21 @@ class RLEIterator:
```java
class RLEIterator {
private int[] encoding;
private int curr;
private int i;
private int j;

public RLEIterator(int[] encoding) {
this.encoding = encoding;
curr = 0;
i = 0;
}

public int next(int n) {
while (i < encoding.length) {
if (curr + n > encoding[i]) {
n -= encoding[i] - curr;
if (encoding[i] - j < n) {
n -= (encoding[i] - j);
i += 2;
curr = 0;
j = 0;
} else {
curr += n;
j += n;
return encoding[i + 1];
}
}
Expand All @@ -129,29 +137,28 @@ class RLEIterator {
```cpp
class RLEIterator {
public:
vector<int> encoding;
int curr;
int i;

RLEIterator(vector<int>& encoding) {
this->encoding = encoding;
this->curr = 0;
this->i = 0;
}

int next(int n) {
while (i < encoding.size()) {
if (curr + n > encoding[i]) {
n -= encoding[i] - curr;
curr = 0;
if (encoding[i] - j < n) {
n -= (encoding[i] - j);
i += 2;
j = 0;
} else {
curr += n;
j += n;
return encoding[i + 1];
}
}
return -1;
}

private:
vector<int> encoding;
int i = 0;
int j = 0;
};

/**
Expand All @@ -166,22 +173,21 @@ public:
```go
type RLEIterator struct {
encoding []int
curr int
i int
i, j int
}

func Constructor(encoding []int) RLEIterator {
return RLEIterator{encoding: encoding, curr: 0, i: 0}
return RLEIterator{encoding, 0, 0}
}

func (this *RLEIterator) Next(n int) int {
for this.i < len(this.encoding) {
if this.curr+n > this.encoding[this.i] {
n -= this.encoding[this.i] - this.curr
this.curr = 0
if this.encoding[this.i]-this.j < n {
n -= (this.encoding[this.i] - this.j)
this.i += 2
this.j = 0
} else {
this.curr += n
this.j += n
return this.encoding[this.i+1]
}
}
Expand All @@ -195,6 +201,42 @@ func (this *RLEIterator) Next(n int) int {
*/
```

### **TypeScript**

```ts
class RLEIterator {
private encoding: number[];
private i: number;
private j: number;

constructor(encoding: number[]) {
this.encoding = encoding;
this.i = 0;
this.j = 0;
}

next(n: number): number {
while (this.i < this.encoding.length) {
if (this.encoding[this.i] - this.j < n) {
n -= this.encoding[this.i] - this.j;
this.i += 2;
this.j = 0;
} else {
this.j += n;
return this.encoding[this.i + 1];
}
}
return -1;
}
}

/**
* Your RLEIterator object will be instantiated and called as such:
* var obj = new RLEIterator(encoding)
* var param_1 = obj.next(n)
*/
```

### **...**

```
Expand Down
100 changes: 71 additions & 29 deletions solution/0900-0999/0900.RLE Iterator/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ but the second term did not exist. Since the last term exhausted does not exist,

## Solutions

**Solution 1: Maintain Two Pointers**

We define two pointers $i$ and $j$, where pointer $i$ points to the current run-length encoding being read, and pointer $j$ points to which character in the current run-length encoding is being read. Initially, $i = 0$, $j = 0$.

Each time we call `next(n)`, we judge whether the remaining number of characters in the current run-length encoding $encoding[i] - j$ is less than $n$. If it is, we subtract $n$ by $encoding[i] - j$, add $2$ to $i$, and set $j$ to $0$, then continue to judge the next run-length encoding. If it is not, we add $n$ to $j$ and return $encoding[i + 1]$.

If $i$ exceeds the length of the run-length encoding and there is still no return value, it means that there are no remaining elements to be exhausted, and we return $-1$.

The time complexity is $O(n + q)$, and the space complexity is $O(n)$. Here, $n$ is the length of the run-length encoding, and $q$ is the number of times `next(n)` is called.

<!-- tabs:start -->

### **Python3**
Expand All @@ -60,16 +70,16 @@ class RLEIterator:
def __init__(self, encoding: List[int]):
self.encoding = encoding
self.i = 0
self.curr = 0
self.j = 0

def next(self, n: int) -> int:
while self.i < len(self.encoding):
if self.curr + n > self.encoding[self.i]:
n -= self.encoding[self.i] - self.curr
self.curr = 0
if self.encoding[self.i] - self.j < n:
n -= self.encoding[self.i] - self.j
self.i += 2
self.j = 0
else:
self.curr += n
self.j += n
return self.encoding[self.i + 1]
return -1

Expand All @@ -84,23 +94,21 @@ class RLEIterator:
```java
class RLEIterator {
private int[] encoding;
private int curr;
private int i;
private int j;

public RLEIterator(int[] encoding) {
this.encoding = encoding;
curr = 0;
i = 0;
}

public int next(int n) {
while (i < encoding.length) {
if (curr + n > encoding[i]) {
n -= encoding[i] - curr;
if (encoding[i] - j < n) {
n -= (encoding[i] - j);
i += 2;
curr = 0;
j = 0;
} else {
curr += n;
j += n;
return encoding[i + 1];
}
}
Expand All @@ -120,29 +128,28 @@ class RLEIterator {
```cpp
class RLEIterator {
public:
vector<int> encoding;
int curr;
int i;

RLEIterator(vector<int>& encoding) {
this->encoding = encoding;
this->curr = 0;
this->i = 0;
}

int next(int n) {
while (i < encoding.size()) {
if (curr + n > encoding[i]) {
n -= encoding[i] - curr;
curr = 0;
if (encoding[i] - j < n) {
n -= (encoding[i] - j);
i += 2;
j = 0;
} else {
curr += n;
j += n;
return encoding[i + 1];
}
}
return -1;
}

private:
vector<int> encoding;
int i = 0;
int j = 0;
};

/**
Expand All @@ -157,22 +164,21 @@ public:
```go
type RLEIterator struct {
encoding []int
curr int
i int
i, j int
}

func Constructor(encoding []int) RLEIterator {
return RLEIterator{encoding: encoding, curr: 0, i: 0}
return RLEIterator{encoding, 0, 0}
}

func (this *RLEIterator) Next(n int) int {
for this.i < len(this.encoding) {
if this.curr+n > this.encoding[this.i] {
n -= this.encoding[this.i] - this.curr
this.curr = 0
if this.encoding[this.i]-this.j < n {
n -= (this.encoding[this.i] - this.j)
this.i += 2
this.j = 0
} else {
this.curr += n
this.j += n
return this.encoding[this.i+1]
}
}
Expand All @@ -186,6 +192,42 @@ func (this *RLEIterator) Next(n int) int {
*/
```

### **TypeScript**

```ts
class RLEIterator {
private encoding: number[];
private i: number;
private j: number;

constructor(encoding: number[]) {
this.encoding = encoding;
this.i = 0;
this.j = 0;
}

next(n: number): number {
while (this.i < this.encoding.length) {
if (this.encoding[this.i] - this.j < n) {
n -= this.encoding[this.i] - this.j;
this.i += 2;
this.j = 0;
} else {
this.j += n;
return this.encoding[this.i + 1];
}
}
return -1;
}
}

/**
* Your RLEIterator object will be instantiated and called as such:
* var obj = new RLEIterator(encoding)
* var param_1 = obj.next(n)
*/
```

### **...**

```
Expand Down
Loading