Skip to content

feat: add solutions to lc problem: No.3465 #4103

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 1 commit into from
Feb 24, 2025
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
93 changes: 48 additions & 45 deletions solution/1600-1699/1656.Design an Ordered Stream/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,27 @@ os.insert(4, "ddddd"); // 插入 (4, "ddddd"),返回 ["ddddd", "eeeee"]

<!-- solution:start -->

### 方法一
### 方法一:数组模拟

我们可以使用一个长度为 $n + 1$ 的数组 $\textit{data}$ 来模拟这个流,其中 $\textit{data}[i]$ 表示 $\textit{id} = i$ 的值。同时,我们使用一个指针 $\textit{ptr}$ 来表示当前的位置。初始时 $\textit{ptr} = 1$。

在插入一个新的 $(\textit{idKey}, \textit{value})$ 对时,我们将 $\textit{data}[\textit{idKey}]$ 更新为 $\textit{value}$。然后,我们从 $\textit{ptr}$ 开始,依次将 $\textit{data}[\textit{ptr}]$ 加入答案中,直到 $\textit{data}[\textit{ptr}]$ 为空。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数据流的长度。

<!-- tabs:start -->

#### Python3

```python
class OrderedStream:

def __init__(self, n: int):
self.data = [None] * n
self.ptr = 0
self.ptr = 1
self.data = [None] * (n + 1)

def insert(self, idKey: int, value: str) -> List[str]:
self.data[idKey - 1] = value
self.data[idKey] = value
ans = []
while self.ptr < len(self.data) and self.data[self.ptr]:
ans.append(self.data[self.ptr])
Expand All @@ -110,16 +117,15 @@ class OrderedStream:

```java
class OrderedStream {
private int ptr = 1;
private String[] data;
private int ptr;

public OrderedStream(int n) {
data = new String[n];
ptr = 0;
data = new String[n + 1];
}

public List<String> insert(int idKey, String value) {
data[idKey - 1] = value;
data[idKey] = value;
List<String> ans = new ArrayList<>();
while (ptr < data.length && data[ptr] != null) {
ans.add(data[ptr++]);
Expand All @@ -140,19 +146,23 @@ class OrderedStream {
```cpp
class OrderedStream {
public:
vector<string> data;
int ptr = 0;

OrderedStream(int n) {
data.resize(n, "");
ptr = 1;
data = vector<string>(n + 1);
}

vector<string> insert(int idKey, string value) {
data[idKey - 1] = value;
data[idKey] = value;
vector<string> ans;
while (ptr < data.size() && data[ptr] != "") ans.push_back(data[ptr++]);
while (ptr < data.size() && !data[ptr].empty()) {
ans.push_back(data[ptr++]);
}
return ans;
}

private:
int ptr;
vector<string> data;
};

/**
Expand All @@ -166,17 +176,19 @@ public:

```go
type OrderedStream struct {
data []string
ptr int
data []string
}

func Constructor(n int) OrderedStream {
data := make([]string, n)
return OrderedStream{data, 0}
return OrderedStream{
ptr: 1,
data: make([]string, n+1),
}
}

func (this *OrderedStream) Insert(idKey int, value string) []string {
this.data[idKey-1] = value
this.data[idKey] = value
var ans []string
for this.ptr < len(this.data) && this.data[this.ptr] != "" {
ans = append(ans, this.data[this.ptr])
Expand All @@ -197,21 +209,20 @@ func (this *OrderedStream) Insert(idKey int, value string) []string {
```ts
class OrderedStream {
private ptr: number;
private vals: string[];
private data: string[];

constructor(n: number) {
this.ptr = 0;
this.vals = new Array(n);
this.ptr = 1;
this.data = Array(n + 1);
}

insert(idKey: number, value: string): string[] {
this.vals[idKey - 1] = value;
const res = [];
while (this.vals[this.ptr] != null) {
res.push(this.vals[this.ptr]);
this.ptr++;
this.data[idKey] = value;
const ans: string[] = [];
while (this.data[this.ptr]) {
ans.push(this.data[this.ptr++]);
}
return res;
return ans;
}
}

Expand All @@ -227,33 +238,25 @@ class OrderedStream {
```rust
struct OrderedStream {
ptr: usize,
vals: Vec<Option<String>>,
data: Vec<Option<String>>,
}

/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl OrderedStream {
fn new(n: i32) -> Self {
Self {
ptr: 0,
vals: vec![None; n as usize],
OrderedStream {
ptr: 1,
data: vec![None; (n + 1) as usize],
}
}

fn insert(&mut self, id_key: i32, value: String) -> Vec<String> {
self.vals[(id_key - 1) as usize] = Some(value);
let mut res = Vec::new();
while self.ptr < self.vals.len() {
if let Some(s) = &self.vals[self.ptr] {
res.push(s.clone());
self.ptr += 1;
} else {
break;
}
self.data[id_key as usize] = Some(value);
let mut ans = Vec::new();
while self.ptr < self.data.len() && self.data[self.ptr].is_some() {
ans.push(self.data[self.ptr].take().unwrap());
self.ptr += 1;
}
res
ans
}
}
```
Expand Down
93 changes: 48 additions & 45 deletions solution/1600-1699/1656.Design an Ordered Stream/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,27 @@ os.insert(4, &quot;ddddd&quot;); // Inserts (4, &quot;ddddd&quot;), returns [&qu

<!-- solution:start -->

### Solution 1
### Solution 1: Array Simulation

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$.

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.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the data stream.

<!-- tabs:start -->

#### Python3

```python
class OrderedStream:

def __init__(self, n: int):
self.data = [None] * n
self.ptr = 0
self.ptr = 1
self.data = [None] * (n + 1)

def insert(self, idKey: int, value: str) -> List[str]:
self.data[idKey - 1] = value
self.data[idKey] = value
ans = []
while self.ptr < len(self.data) and self.data[self.ptr]:
ans.append(self.data[self.ptr])
Expand All @@ -105,16 +112,15 @@ class OrderedStream:

```java
class OrderedStream {
private int ptr = 1;
private String[] data;
private int ptr;

public OrderedStream(int n) {
data = new String[n];
ptr = 0;
data = new String[n + 1];
}

public List<String> insert(int idKey, String value) {
data[idKey - 1] = value;
data[idKey] = value;
List<String> ans = new ArrayList<>();
while (ptr < data.length && data[ptr] != null) {
ans.add(data[ptr++]);
Expand All @@ -135,19 +141,23 @@ class OrderedStream {
```cpp
class OrderedStream {
public:
vector<string> data;
int ptr = 0;

OrderedStream(int n) {
data.resize(n, "");
ptr = 1;
data = vector<string>(n + 1);
}

vector<string> insert(int idKey, string value) {
data[idKey - 1] = value;
data[idKey] = value;
vector<string> ans;
while (ptr < data.size() && data[ptr] != "") ans.push_back(data[ptr++]);
while (ptr < data.size() && !data[ptr].empty()) {
ans.push_back(data[ptr++]);
}
return ans;
}

private:
int ptr;
vector<string> data;
};

/**
Expand All @@ -161,17 +171,19 @@ public:

```go
type OrderedStream struct {
data []string
ptr int
data []string
}

func Constructor(n int) OrderedStream {
data := make([]string, n)
return OrderedStream{data, 0}
return OrderedStream{
ptr: 1,
data: make([]string, n+1),
}
}

func (this *OrderedStream) Insert(idKey int, value string) []string {
this.data[idKey-1] = value
this.data[idKey] = value
var ans []string
for this.ptr < len(this.data) && this.data[this.ptr] != "" {
ans = append(ans, this.data[this.ptr])
Expand All @@ -192,21 +204,20 @@ func (this *OrderedStream) Insert(idKey int, value string) []string {
```ts
class OrderedStream {
private ptr: number;
private vals: string[];
private data: string[];

constructor(n: number) {
this.ptr = 0;
this.vals = new Array(n);
this.ptr = 1;
this.data = Array(n + 1);
}

insert(idKey: number, value: string): string[] {
this.vals[idKey - 1] = value;
const res = [];
while (this.vals[this.ptr] != null) {
res.push(this.vals[this.ptr]);
this.ptr++;
this.data[idKey] = value;
const ans: string[] = [];
while (this.data[this.ptr]) {
ans.push(this.data[this.ptr++]);
}
return res;
return ans;
}
}

Expand All @@ -222,33 +233,25 @@ class OrderedStream {
```rust
struct OrderedStream {
ptr: usize,
vals: Vec<Option<String>>,
data: Vec<Option<String>>,
}

/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl OrderedStream {
fn new(n: i32) -> Self {
Self {
ptr: 0,
vals: vec![None; n as usize],
OrderedStream {
ptr: 1,
data: vec![None; (n + 1) as usize],
}
}

fn insert(&mut self, id_key: i32, value: String) -> Vec<String> {
self.vals[(id_key - 1) as usize] = Some(value);
let mut res = Vec::new();
while self.ptr < self.vals.len() {
if let Some(s) = &self.vals[self.ptr] {
res.push(s.clone());
self.ptr += 1;
} else {
break;
}
self.data[id_key as usize] = Some(value);
let mut ans = Vec::new();
while self.ptr < self.data.len() && self.data[self.ptr].is_some() {
ans.push(self.data[self.ptr].take().unwrap());
self.ptr += 1;
}
res
ans
}
}
```
Expand Down
18 changes: 11 additions & 7 deletions solution/1600-1699/1656.Design an Ordered Stream/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
class OrderedStream {
public:
vector<string> data;
int ptr = 0;

OrderedStream(int n) {
data.resize(n, "");
ptr = 1;
data = vector<string>(n + 1);
}

vector<string> insert(int idKey, string value) {
data[idKey - 1] = value;
data[idKey] = value;
vector<string> ans;
while (ptr < data.size() && data[ptr] != "") ans.push_back(data[ptr++]);
while (ptr < data.size() && !data[ptr].empty()) {
ans.push_back(data[ptr++]);
}
return ans;
}

private:
int ptr;
vector<string> data;
};

/**
* Your OrderedStream object will be instantiated and called as such:
* OrderedStream* obj = new OrderedStream(n);
* vector<string> param_1 = obj->insert(idKey,value);
*/
*/
Loading