Skip to content

Commit 394bbc0

Browse files
authored
feat: add solutions to lc problem: No.3465 (doocs#4103)
No.3465.Find Products with Valid Serial Numbers
1 parent 0675550 commit 394bbc0

File tree

17 files changed

+435
-138
lines changed

17 files changed

+435
-138
lines changed

solution/1600-1699/1656.Design an Ordered Stream/README.md

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,27 @@ os.insert(4, "ddddd"); // 插入 (4, "ddddd"),返回 ["ddddd", "eeeee"]
8080

8181
<!-- solution:start -->
8282

83-
### 方法一
83+
### 方法一:数组模拟
84+
85+
我们可以使用一个长度为 $n + 1$ 的数组 $\textit{data}$ 来模拟这个流,其中 $\textit{data}[i]$ 表示 $\textit{id} = i$ 的值。同时,我们使用一个指针 $\textit{ptr}$ 来表示当前的位置。初始时 $\textit{ptr} = 1$。
86+
87+
在插入一个新的 $(\textit{idKey}, \textit{value})$ 对时,我们将 $\textit{data}[\textit{idKey}]$ 更新为 $\textit{value}$。然后,我们从 $\textit{ptr}$ 开始,依次将 $\textit{data}[\textit{ptr}]$ 加入答案中,直到 $\textit{data}[\textit{ptr}]$ 为空。
88+
89+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数据流的长度。
8490

8591
<!-- tabs:start -->
8692

8793
#### Python3
8894

8995
```python
9096
class OrderedStream:
97+
9198
def __init__(self, n: int):
92-
self.data = [None] * n
93-
self.ptr = 0
99+
self.ptr = 1
100+
self.data = [None] * (n + 1)
94101

95102
def insert(self, idKey: int, value: str) -> List[str]:
96-
self.data[idKey - 1] = value
103+
self.data[idKey] = value
97104
ans = []
98105
while self.ptr < len(self.data) and self.data[self.ptr]:
99106
ans.append(self.data[self.ptr])
@@ -110,16 +117,15 @@ class OrderedStream:
110117

111118
```java
112119
class OrderedStream {
120+
private int ptr = 1;
113121
private String[] data;
114-
private int ptr;
115122

116123
public OrderedStream(int n) {
117-
data = new String[n];
118-
ptr = 0;
124+
data = new String[n + 1];
119125
}
120126

121127
public List<String> insert(int idKey, String value) {
122-
data[idKey - 1] = value;
128+
data[idKey] = value;
123129
List<String> ans = new ArrayList<>();
124130
while (ptr < data.length && data[ptr] != null) {
125131
ans.add(data[ptr++]);
@@ -140,19 +146,23 @@ class OrderedStream {
140146
```cpp
141147
class OrderedStream {
142148
public:
143-
vector<string> data;
144-
int ptr = 0;
145-
146149
OrderedStream(int n) {
147-
data.resize(n, "");
150+
ptr = 1;
151+
data = vector<string>(n + 1);
148152
}
149153

150154
vector<string> insert(int idKey, string value) {
151-
data[idKey - 1] = value;
155+
data[idKey] = value;
152156
vector<string> ans;
153-
while (ptr < data.size() && data[ptr] != "") ans.push_back(data[ptr++]);
157+
while (ptr < data.size() && !data[ptr].empty()) {
158+
ans.push_back(data[ptr++]);
159+
}
154160
return ans;
155161
}
162+
163+
private:
164+
int ptr;
165+
vector<string> data;
156166
};
157167

158168
/**
@@ -166,17 +176,19 @@ public:
166176

167177
```go
168178
type OrderedStream struct {
169-
data []string
170179
ptr int
180+
data []string
171181
}
172182

173183
func Constructor(n int) OrderedStream {
174-
data := make([]string, n)
175-
return OrderedStream{data, 0}
184+
return OrderedStream{
185+
ptr: 1,
186+
data: make([]string, n+1),
187+
}
176188
}
177189

178190
func (this *OrderedStream) Insert(idKey int, value string) []string {
179-
this.data[idKey-1] = value
191+
this.data[idKey] = value
180192
var ans []string
181193
for this.ptr < len(this.data) && this.data[this.ptr] != "" {
182194
ans = append(ans, this.data[this.ptr])
@@ -197,21 +209,20 @@ func (this *OrderedStream) Insert(idKey int, value string) []string {
197209
```ts
198210
class OrderedStream {
199211
private ptr: number;
200-
private vals: string[];
212+
private data: string[];
201213

202214
constructor(n: number) {
203-
this.ptr = 0;
204-
this.vals = new Array(n);
215+
this.ptr = 1;
216+
this.data = Array(n + 1);
205217
}
206218

207219
insert(idKey: number, value: string): string[] {
208-
this.vals[idKey - 1] = value;
209-
const res = [];
210-
while (this.vals[this.ptr] != null) {
211-
res.push(this.vals[this.ptr]);
212-
this.ptr++;
220+
this.data[idKey] = value;
221+
const ans: string[] = [];
222+
while (this.data[this.ptr]) {
223+
ans.push(this.data[this.ptr++]);
213224
}
214-
return res;
225+
return ans;
215226
}
216227
}
217228

@@ -227,33 +238,25 @@ class OrderedStream {
227238
```rust
228239
struct OrderedStream {
229240
ptr: usize,
230-
vals: Vec<Option<String>>,
241+
data: Vec<Option<String>>,
231242
}
232243

233-
/**
234-
* `&self` means the method takes an immutable reference.
235-
* If you need a mutable reference, change it to `&mut self` instead.
236-
*/
237244
impl OrderedStream {
238245
fn new(n: i32) -> Self {
239-
Self {
240-
ptr: 0,
241-
vals: vec![None; n as usize],
246+
OrderedStream {
247+
ptr: 1,
248+
data: vec![None; (n + 1) as usize],
242249
}
243250
}
244251

245252
fn insert(&mut self, id_key: i32, value: String) -> Vec<String> {
246-
self.vals[(id_key - 1) as usize] = Some(value);
247-
let mut res = Vec::new();
248-
while self.ptr < self.vals.len() {
249-
if let Some(s) = &self.vals[self.ptr] {
250-
res.push(s.clone());
251-
self.ptr += 1;
252-
} else {
253-
break;
254-
}
253+
self.data[id_key as usize] = Some(value);
254+
let mut ans = Vec::new();
255+
while self.ptr < self.data.len() && self.data[self.ptr].is_some() {
256+
ans.push(self.data[self.ptr].take().unwrap());
257+
self.ptr += 1;
255258
}
256-
res
259+
ans
257260
}
258261
}
259262
```

solution/1600-1699/1656.Design an Ordered Stream/README_EN.md

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,27 @@ os.insert(4, &quot;ddddd&quot;); // Inserts (4, &quot;ddddd&quot;), returns [&qu
7575

7676
<!-- solution:start -->
7777

78-
### Solution 1
78+
### Solution 1: Array Simulation
79+
80+
We can use an array $\textit{data}$ of length $n + 1$ to simulate this stream, where $\textit{data}[i]$ represents the value of $\textit{id} = i$. At the same time, we use a pointer $\textit{ptr}$ to represent the current position. Initially, $\textit{ptr} = 1$.
81+
82+
When inserting a new $(\textit{idKey}, \textit{value})$ pair, we update $\textit{data}[\textit{idKey}]$ to $\textit{value}$. Then, starting from $\textit{ptr}$, we sequentially add $\textit{data}[\textit{ptr}]$ to the answer until $\textit{data}[\textit{ptr}]$ is empty.
83+
84+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the data stream.
7985

8086
<!-- tabs:start -->
8187

8288
#### Python3
8389

8490
```python
8591
class OrderedStream:
92+
8693
def __init__(self, n: int):
87-
self.data = [None] * n
88-
self.ptr = 0
94+
self.ptr = 1
95+
self.data = [None] * (n + 1)
8996

9097
def insert(self, idKey: int, value: str) -> List[str]:
91-
self.data[idKey - 1] = value
98+
self.data[idKey] = value
9299
ans = []
93100
while self.ptr < len(self.data) and self.data[self.ptr]:
94101
ans.append(self.data[self.ptr])
@@ -105,16 +112,15 @@ class OrderedStream:
105112

106113
```java
107114
class OrderedStream {
115+
private int ptr = 1;
108116
private String[] data;
109-
private int ptr;
110117

111118
public OrderedStream(int n) {
112-
data = new String[n];
113-
ptr = 0;
119+
data = new String[n + 1];
114120
}
115121

116122
public List<String> insert(int idKey, String value) {
117-
data[idKey - 1] = value;
123+
data[idKey] = value;
118124
List<String> ans = new ArrayList<>();
119125
while (ptr < data.length && data[ptr] != null) {
120126
ans.add(data[ptr++]);
@@ -135,19 +141,23 @@ class OrderedStream {
135141
```cpp
136142
class OrderedStream {
137143
public:
138-
vector<string> data;
139-
int ptr = 0;
140-
141144
OrderedStream(int n) {
142-
data.resize(n, "");
145+
ptr = 1;
146+
data = vector<string>(n + 1);
143147
}
144148

145149
vector<string> insert(int idKey, string value) {
146-
data[idKey - 1] = value;
150+
data[idKey] = value;
147151
vector<string> ans;
148-
while (ptr < data.size() && data[ptr] != "") ans.push_back(data[ptr++]);
152+
while (ptr < data.size() && !data[ptr].empty()) {
153+
ans.push_back(data[ptr++]);
154+
}
149155
return ans;
150156
}
157+
158+
private:
159+
int ptr;
160+
vector<string> data;
151161
};
152162

153163
/**
@@ -161,17 +171,19 @@ public:
161171

162172
```go
163173
type OrderedStream struct {
164-
data []string
165174
ptr int
175+
data []string
166176
}
167177

168178
func Constructor(n int) OrderedStream {
169-
data := make([]string, n)
170-
return OrderedStream{data, 0}
179+
return OrderedStream{
180+
ptr: 1,
181+
data: make([]string, n+1),
182+
}
171183
}
172184

173185
func (this *OrderedStream) Insert(idKey int, value string) []string {
174-
this.data[idKey-1] = value
186+
this.data[idKey] = value
175187
var ans []string
176188
for this.ptr < len(this.data) && this.data[this.ptr] != "" {
177189
ans = append(ans, this.data[this.ptr])
@@ -192,21 +204,20 @@ func (this *OrderedStream) Insert(idKey int, value string) []string {
192204
```ts
193205
class OrderedStream {
194206
private ptr: number;
195-
private vals: string[];
207+
private data: string[];
196208

197209
constructor(n: number) {
198-
this.ptr = 0;
199-
this.vals = new Array(n);
210+
this.ptr = 1;
211+
this.data = Array(n + 1);
200212
}
201213

202214
insert(idKey: number, value: string): string[] {
203-
this.vals[idKey - 1] = value;
204-
const res = [];
205-
while (this.vals[this.ptr] != null) {
206-
res.push(this.vals[this.ptr]);
207-
this.ptr++;
215+
this.data[idKey] = value;
216+
const ans: string[] = [];
217+
while (this.data[this.ptr]) {
218+
ans.push(this.data[this.ptr++]);
208219
}
209-
return res;
220+
return ans;
210221
}
211222
}
212223

@@ -222,33 +233,25 @@ class OrderedStream {
222233
```rust
223234
struct OrderedStream {
224235
ptr: usize,
225-
vals: Vec<Option<String>>,
236+
data: Vec<Option<String>>,
226237
}
227238

228-
/**
229-
* `&self` means the method takes an immutable reference.
230-
* If you need a mutable reference, change it to `&mut self` instead.
231-
*/
232239
impl OrderedStream {
233240
fn new(n: i32) -> Self {
234-
Self {
235-
ptr: 0,
236-
vals: vec![None; n as usize],
241+
OrderedStream {
242+
ptr: 1,
243+
data: vec![None; (n + 1) as usize],
237244
}
238245
}
239246

240247
fn insert(&mut self, id_key: i32, value: String) -> Vec<String> {
241-
self.vals[(id_key - 1) as usize] = Some(value);
242-
let mut res = Vec::new();
243-
while self.ptr < self.vals.len() {
244-
if let Some(s) = &self.vals[self.ptr] {
245-
res.push(s.clone());
246-
self.ptr += 1;
247-
} else {
248-
break;
249-
}
248+
self.data[id_key as usize] = Some(value);
249+
let mut ans = Vec::new();
250+
while self.ptr < self.data.len() && self.data[self.ptr].is_some() {
251+
ans.push(self.data[self.ptr].take().unwrap());
252+
self.ptr += 1;
250253
}
251-
res
254+
ans
252255
}
253256
}
254257
```
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
class OrderedStream {
22
public:
3-
vector<string> data;
4-
int ptr = 0;
5-
63
OrderedStream(int n) {
7-
data.resize(n, "");
4+
ptr = 1;
5+
data = vector<string>(n + 1);
86
}
97

108
vector<string> insert(int idKey, string value) {
11-
data[idKey - 1] = value;
9+
data[idKey] = value;
1210
vector<string> ans;
13-
while (ptr < data.size() && data[ptr] != "") ans.push_back(data[ptr++]);
11+
while (ptr < data.size() && !data[ptr].empty()) {
12+
ans.push_back(data[ptr++]);
13+
}
1414
return ans;
1515
}
16+
17+
private:
18+
int ptr;
19+
vector<string> data;
1620
};
1721

1822
/**
1923
* Your OrderedStream object will be instantiated and called as such:
2024
* OrderedStream* obj = new OrderedStream(n);
2125
* vector<string> param_1 = obj->insert(idKey,value);
22-
*/
26+
*/

0 commit comments

Comments
 (0)