Skip to content

Commit 894bb7d

Browse files
authored
feat: add solutions to lc problems: No.0899~0908 (#2108)
* No.0899.Orderly Queue * No.0900.RLE Iterator * No.0901.Online Stock Span * No.0903.Valid Permutations for DI Sequence * No.0904.Fruit Into Baskets * No.0905.Sort Array By Parity
1 parent 5d6b9e7 commit 894bb7d

File tree

25 files changed

+623
-327
lines changed

25 files changed

+623
-327
lines changed

solution/0800-0899/0899.Orderly Queue/README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@
4949

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

52-
**方法一:分类判断**
52+
**方法一:分情况判断**
5353

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

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

58-
因此,只要 $k\gt1$,我们就能够交换字符串中的任何两个相邻字符,最终得到一个升序排列的字符串。
58+
因此,只要 $k \gt 1$,我们就能够交换字符串中的任何两个相邻字符,最终得到一个升序排列的字符串。
59+
60+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是字符串的长度。
5961

6062
<!-- tabs:start -->
6163

solution/0800-0899/0899.Orderly Queue/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ In the second move, we move the 3<sup>rd</sup> character &#39;c&#39; to the end,
3939

4040
## Solutions
4141

42+
**Preface**
43+
44+
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.
45+
46+
**Solution 1: Case-by-case Judgment**
47+
48+
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.
49+
50+
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$.
51+
52+
Therefore, as long as $k > 1$, we can swap any two adjacent characters in the string, eventually obtaining a string sorted in ascending order.
53+
54+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.
55+
4256
<!-- tabs:start -->
4357

4458
### **Python3**

solution/0900-0999/0900.RLE Iterator/README.md

+71-29
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ rLEIterator.next(2); // 耗去序列的 2 个项,返回 -1。 这是由于第
5656

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

59+
**方法一:维护两个指针**
60+
61+
我们定义两个指针 $i$ 和 $j$,其中指针 $i$ 指向当前读取的游程编码,指针 $j$ 指向当前读取的游程编码中的第几个字符。初始时 $i = 0$, $j = 0$。
62+
63+
每次调用 `next(n)` 时,我们判断当前游程编码中剩余的字符数 $encoding[i] - j$ 是否小于 $n$,若是,则将 $n$ 减去 $encoding[i] - j$,并将 $i$ 加 $2$,$j$ 置为 $0$,然后继续判断下一个游程编码;若不是,则将 $j$ 加 $n$,并返回 $encoding[i + 1]$。
64+
65+
若 $i$ 超出了游程编码的长度,依然没有返回值,则说明没有剩余的元素要耗尽,返回 $-1$。
66+
67+
时间复杂度 $O(n + q)$,空间复杂度 $O(n)$。其中 $n$ 是游程编码的长度,而 $q$ 是调用 `next(n)` 的次数。
68+
5969
<!-- tabs:start -->
6070

6171
### **Python3**
@@ -67,16 +77,16 @@ class RLEIterator:
6777
def __init__(self, encoding: List[int]):
6878
self.encoding = encoding
6979
self.i = 0
70-
self.curr = 0
80+
self.j = 0
7181

7282
def next(self, n: int) -> int:
7383
while self.i < len(self.encoding):
74-
if self.curr + n > self.encoding[self.i]:
75-
n -= self.encoding[self.i] - self.curr
76-
self.curr = 0
84+
if self.encoding[self.i] - self.j < n:
85+
n -= self.encoding[self.i] - self.j
7786
self.i += 2
87+
self.j = 0
7888
else:
79-
self.curr += n
89+
self.j += n
8090
return self.encoding[self.i + 1]
8191
return -1
8292

@@ -93,23 +103,21 @@ class RLEIterator:
93103
```java
94104
class RLEIterator {
95105
private int[] encoding;
96-
private int curr;
97106
private int i;
107+
private int j;
98108

99109
public RLEIterator(int[] encoding) {
100110
this.encoding = encoding;
101-
curr = 0;
102-
i = 0;
103111
}
104112

105113
public int next(int n) {
106114
while (i < encoding.length) {
107-
if (curr + n > encoding[i]) {
108-
n -= encoding[i] - curr;
115+
if (encoding[i] - j < n) {
116+
n -= (encoding[i] - j);
109117
i += 2;
110-
curr = 0;
118+
j = 0;
111119
} else {
112-
curr += n;
120+
j += n;
113121
return encoding[i + 1];
114122
}
115123
}
@@ -129,29 +137,28 @@ class RLEIterator {
129137
```cpp
130138
class RLEIterator {
131139
public:
132-
vector<int> encoding;
133-
int curr;
134-
int i;
135-
136140
RLEIterator(vector<int>& encoding) {
137141
this->encoding = encoding;
138-
this->curr = 0;
139-
this->i = 0;
140142
}
141143

142144
int next(int n) {
143145
while (i < encoding.size()) {
144-
if (curr + n > encoding[i]) {
145-
n -= encoding[i] - curr;
146-
curr = 0;
146+
if (encoding[i] - j < n) {
147+
n -= (encoding[i] - j);
147148
i += 2;
149+
j = 0;
148150
} else {
149-
curr += n;
151+
j += n;
150152
return encoding[i + 1];
151153
}
152154
}
153155
return -1;
154156
}
157+
158+
private:
159+
vector<int> encoding;
160+
int i = 0;
161+
int j = 0;
155162
};
156163

157164
/**
@@ -166,22 +173,21 @@ public:
166173
```go
167174
type RLEIterator struct {
168175
encoding []int
169-
curr int
170-
i int
176+
i, j int
171177
}
172178

173179
func Constructor(encoding []int) RLEIterator {
174-
return RLEIterator{encoding: encoding, curr: 0, i: 0}
180+
return RLEIterator{encoding, 0, 0}
175181
}
176182

177183
func (this *RLEIterator) Next(n int) int {
178184
for this.i < len(this.encoding) {
179-
if this.curr+n > this.encoding[this.i] {
180-
n -= this.encoding[this.i] - this.curr
181-
this.curr = 0
185+
if this.encoding[this.i]-this.j < n {
186+
n -= (this.encoding[this.i] - this.j)
182187
this.i += 2
188+
this.j = 0
183189
} else {
184-
this.curr += n
190+
this.j += n
185191
return this.encoding[this.i+1]
186192
}
187193
}
@@ -195,6 +201,42 @@ func (this *RLEIterator) Next(n int) int {
195201
*/
196202
```
197203

204+
### **TypeScript**
205+
206+
```ts
207+
class RLEIterator {
208+
private encoding: number[];
209+
private i: number;
210+
private j: number;
211+
212+
constructor(encoding: number[]) {
213+
this.encoding = encoding;
214+
this.i = 0;
215+
this.j = 0;
216+
}
217+
218+
next(n: number): number {
219+
while (this.i < this.encoding.length) {
220+
if (this.encoding[this.i] - this.j < n) {
221+
n -= this.encoding[this.i] - this.j;
222+
this.i += 2;
223+
this.j = 0;
224+
} else {
225+
this.j += n;
226+
return this.encoding[this.i + 1];
227+
}
228+
}
229+
return -1;
230+
}
231+
}
232+
233+
/**
234+
* Your RLEIterator object will be instantiated and called as such:
235+
* var obj = new RLEIterator(encoding)
236+
* var param_1 = obj.next(n)
237+
*/
238+
```
239+
198240
### **...**
199241

200242
```

solution/0900-0999/0900.RLE Iterator/README_EN.md

+71-29
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ but the second term did not exist. Since the last term exhausted does not exist,
5151

5252
## Solutions
5353

54+
**Solution 1: Maintain Two Pointers**
55+
56+
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$.
57+
58+
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]$.
59+
60+
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$.
61+
62+
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.
63+
5464
<!-- tabs:start -->
5565

5666
### **Python3**
@@ -60,16 +70,16 @@ class RLEIterator:
6070
def __init__(self, encoding: List[int]):
6171
self.encoding = encoding
6272
self.i = 0
63-
self.curr = 0
73+
self.j = 0
6474

6575
def next(self, n: int) -> int:
6676
while self.i < len(self.encoding):
67-
if self.curr + n > self.encoding[self.i]:
68-
n -= self.encoding[self.i] - self.curr
69-
self.curr = 0
77+
if self.encoding[self.i] - self.j < n:
78+
n -= self.encoding[self.i] - self.j
7079
self.i += 2
80+
self.j = 0
7181
else:
72-
self.curr += n
82+
self.j += n
7383
return self.encoding[self.i + 1]
7484
return -1
7585

@@ -84,23 +94,21 @@ class RLEIterator:
8494
```java
8595
class RLEIterator {
8696
private int[] encoding;
87-
private int curr;
8897
private int i;
98+
private int j;
8999

90100
public RLEIterator(int[] encoding) {
91101
this.encoding = encoding;
92-
curr = 0;
93-
i = 0;
94102
}
95103

96104
public int next(int n) {
97105
while (i < encoding.length) {
98-
if (curr + n > encoding[i]) {
99-
n -= encoding[i] - curr;
106+
if (encoding[i] - j < n) {
107+
n -= (encoding[i] - j);
100108
i += 2;
101-
curr = 0;
109+
j = 0;
102110
} else {
103-
curr += n;
111+
j += n;
104112
return encoding[i + 1];
105113
}
106114
}
@@ -120,29 +128,28 @@ class RLEIterator {
120128
```cpp
121129
class RLEIterator {
122130
public:
123-
vector<int> encoding;
124-
int curr;
125-
int i;
126-
127131
RLEIterator(vector<int>& encoding) {
128132
this->encoding = encoding;
129-
this->curr = 0;
130-
this->i = 0;
131133
}
132134

133135
int next(int n) {
134136
while (i < encoding.size()) {
135-
if (curr + n > encoding[i]) {
136-
n -= encoding[i] - curr;
137-
curr = 0;
137+
if (encoding[i] - j < n) {
138+
n -= (encoding[i] - j);
138139
i += 2;
140+
j = 0;
139141
} else {
140-
curr += n;
142+
j += n;
141143
return encoding[i + 1];
142144
}
143145
}
144146
return -1;
145147
}
148+
149+
private:
150+
vector<int> encoding;
151+
int i = 0;
152+
int j = 0;
146153
};
147154

148155
/**
@@ -157,22 +164,21 @@ public:
157164
```go
158165
type RLEIterator struct {
159166
encoding []int
160-
curr int
161-
i int
167+
i, j int
162168
}
163169

164170
func Constructor(encoding []int) RLEIterator {
165-
return RLEIterator{encoding: encoding, curr: 0, i: 0}
171+
return RLEIterator{encoding, 0, 0}
166172
}
167173

168174
func (this *RLEIterator) Next(n int) int {
169175
for this.i < len(this.encoding) {
170-
if this.curr+n > this.encoding[this.i] {
171-
n -= this.encoding[this.i] - this.curr
172-
this.curr = 0
176+
if this.encoding[this.i]-this.j < n {
177+
n -= (this.encoding[this.i] - this.j)
173178
this.i += 2
179+
this.j = 0
174180
} else {
175-
this.curr += n
181+
this.j += n
176182
return this.encoding[this.i+1]
177183
}
178184
}
@@ -186,6 +192,42 @@ func (this *RLEIterator) Next(n int) int {
186192
*/
187193
```
188194

195+
### **TypeScript**
196+
197+
```ts
198+
class RLEIterator {
199+
private encoding: number[];
200+
private i: number;
201+
private j: number;
202+
203+
constructor(encoding: number[]) {
204+
this.encoding = encoding;
205+
this.i = 0;
206+
this.j = 0;
207+
}
208+
209+
next(n: number): number {
210+
while (this.i < this.encoding.length) {
211+
if (this.encoding[this.i] - this.j < n) {
212+
n -= this.encoding[this.i] - this.j;
213+
this.i += 2;
214+
this.j = 0;
215+
} else {
216+
this.j += n;
217+
return this.encoding[this.i + 1];
218+
}
219+
}
220+
return -1;
221+
}
222+
}
223+
224+
/**
225+
* Your RLEIterator object will be instantiated and called as such:
226+
* var obj = new RLEIterator(encoding)
227+
* var param_1 = obj.next(n)
228+
*/
229+
```
230+
189231
### **...**
190232

191233
```

0 commit comments

Comments
 (0)