Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5aa0cea

Browse files
feat: add solutions to lc problem: No.3023 (doocs#2293)
No.3023.Find Pattern in Infinite Stream I
1 parent 03403b9 commit 5aa0cea

File tree

10 files changed

+577
-33
lines changed

10 files changed

+577
-33
lines changed

‎solution/2200-2299/2207.Maximize Number of Subsequences in a String/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [2207. 字符串中最多数目的子字符串](https://leetcode.cn/problems/maximize-number-of-subsequences-in-a-string)
1+
# [2207. 字符串中最多数目的子序列](https://leetcode.cn/problems/maximize-number-of-subsequences-in-a-string)
22

33
[English Version](/solution/2200-2299/2207.Maximize%20Number%20of%20Subsequences%20in%20a%20String/README_EN.md)
44

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
# [3023. Find Pattern in Infinite Stream I](https://leetcode.cn/problems/find-pattern-in-infinite-stream-i)
2+
3+
[English Version](/solution/3000-3099/3023.Find%20Pattern%20in%20Infinite%20Stream%20I/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given a binary array <code>pattern</code> and an object <code>stream</code> of class <code>InfiniteStream</code> representing a <strong>0-indexed</strong> infinite stream of bits.</p>
10+
11+
<p>The class <code>InfiniteStream</code> contains the following function:</p>
12+
13+
<ul>
14+
<li><code>int next()</code>: Reads a <strong>single</strong> bit (which is either <code>0</code> or <code>1</code>) from the stream and returns it.</li>
15+
</ul>
16+
17+
<p>Return <em>the <strong>first starting</strong> index where the pattern matches the bits read from the stream</em>. For example, if the pattern is <code>[1, 0]</code>, the first match is the highlighted part in the stream <code>[0, <strong><u>1, 0</u></strong>, 1, ...]</code>.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> stream = [1,1,1,0,1,1,1,...], pattern = [0,1]
24+
<strong>Output:</strong> 3
25+
<strong>Explanation:</strong> The first occurrence of the pattern [0,1] is highlighted in the stream [1,1,1,<strong><u>0,1</u></strong>,...], which starts at index 3.
26+
</pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> stream = [0,0,0,0,...], pattern = [0]
32+
<strong>Output:</strong> 0
33+
<strong>Explanation:</strong> The first occurrence of the pattern [0] is highlighted in the stream [<strong><u>0</u></strong>,...], which starts at index 0.
34+
</pre>
35+
36+
<p><strong class="example">Example 3:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> stream = [1,0,1,1,0,1,1,0,1,...], pattern = [1,1,0,1]
40+
<strong>Output:</strong> 2
41+
<strong>Explanation:</strong> The first occurrence of the pattern [1,1,0,1] is highlighted in the stream [1,0,<strong><u>1,1,0,1</u></strong>,...], which starts at index 2.
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= pattern.length &lt;= 100</code></li>
49+
<li><code>pattern</code> consists only of <code>0</code> and <code>1</code>.</li>
50+
<li><code>stream</code> consists only of <code>0</code> and <code>1</code>.</li>
51+
<li>The input is generated such that the pattern&#39;s start index exists in the first <code>10<sup>5</sup></code> bits of the stream.</li>
52+
</ul>
53+
54+
## 解法
55+
56+
### 方法一:位运算 + 滑动窗口
57+
58+
我们注意到,数组 $pattern$ 的长度不超过 100ドル,ドル因此,我们可以用两个 64ドル$ 位的整数 $a$ 和 $b$ 来表示 $pattern$ 左右两半的二进制数。
59+
60+
接下来,我们遍历数据流,同样维护两个 64ドル$ 位的整数 $x$ 和 $y$ 表示当前 $pattern$ 长度的窗口的二进制数。如果当前达到了窗口的长度,我们比较 $a$ 和 $x$ 是否相等,以及 $b$ 和 $y$ 是否相等,如果是,我们返回当前数据流的索引即可。
61+
62+
时间复杂度 $O(n + m),ドル其中 $n$ 和 $m$ 分别是数据流和 $pattern$ 的元素个数。空间复杂度 $O(1)$。
63+
64+
<!-- tabs:start -->
65+
66+
```python
67+
# Definition for an infinite stream.
68+
# class InfiniteStream:
69+
# def next(self) -> int:
70+
# pass
71+
class Solution:
72+
def findPattern(
73+
self, stream: Optional["InfiniteStream"], pattern: List[int]
74+
) -> int:
75+
a = b = 0
76+
m = len(pattern)
77+
half = m >> 1
78+
mask1 = (1 << half) - 1
79+
mask2 = (1 << (m - half)) - 1
80+
for i in range(half):
81+
a |= pattern[i] << (half - 1 - i)
82+
for i in range(half, m):
83+
b |= pattern[i] << (m - 1 - i)
84+
x = y = 0
85+
for i in count(1):
86+
v = stream.next()
87+
y = y << 1 | v
88+
v = y >> (m - half) & 1
89+
y &= mask2
90+
x = x << 1 | v
91+
x &= mask1
92+
if i >= m and a == x and b == y:
93+
return i - m
94+
```
95+
96+
```java
97+
/**
98+
* Definition for an infinite stream.
99+
* class InfiniteStream {
100+
* public InfiniteStream(int[] bits);
101+
* public int next();
102+
* }
103+
*/
104+
class Solution {
105+
public int findPattern(InfiniteStream infiniteStream, int[] pattern) {
106+
long a = 0, b = 0;
107+
int m = pattern.length;
108+
int half = m >> 1;
109+
long mask1 = (1L << half) - 1;
110+
long mask2 = (1L << (m - half)) - 1;
111+
for (int i = 0; i < half; ++i) {
112+
a |= (long) pattern[i] << (half - 1 - i);
113+
}
114+
for (int i = half; i < m; ++i) {
115+
b |= (long) pattern[i] << (m - 1 - i);
116+
}
117+
long x = 0, y = 0;
118+
for (int i = 1;; ++i) {
119+
int v = infiniteStream.next();
120+
y = y << 1 | v;
121+
v = (int) ((y >> (m - half)) & 1);
122+
y &= mask2;
123+
x = x << 1 | v;
124+
x &= mask1;
125+
if (i >= m && a == x && b == y) {
126+
return i - m;
127+
}
128+
}
129+
}
130+
}
131+
```
132+
133+
```cpp
134+
/**
135+
* Definition for an infinite stream.
136+
* class InfiniteStream {
137+
* public:
138+
* InfiniteStream(vector<int> bits);
139+
* int next();
140+
* };
141+
*/
142+
class Solution {
143+
public:
144+
int findPattern(InfiniteStream* stream, vector<int>& pattern) {
145+
long long a = 0, b = 0;
146+
int m = pattern.size();
147+
int half = m >> 1;
148+
long long mask1 = (1LL << half) - 1;
149+
long long mask2 = (1LL << (m - half)) - 1;
150+
for (int i = 0; i < half; ++i) {
151+
a |= (long long) pattern[i] << (half - 1 - i);
152+
}
153+
for (int i = half; i < m; ++i) {
154+
b |= (long long) pattern[i] << (m - 1 - i);
155+
}
156+
long x = 0, y = 0;
157+
for (int i = 1;; ++i) {
158+
int v = stream->next();
159+
y = y << 1 | v;
160+
v = (int) ((y >> (m - half)) & 1);
161+
y &= mask2;
162+
x = x << 1 | v;
163+
x &= mask1;
164+
if (i >= m && a == x && b == y) {
165+
return i - m;
166+
}
167+
}
168+
}
169+
};
170+
```
171+
172+
```go
173+
/**
174+
* Definition for an infinite stream.
175+
* type InfiniteStream interface {
176+
* Next() int
177+
* }
178+
*/
179+
func findPattern(stream InfiniteStream, pattern []int) int {
180+
a, b := 0, 0
181+
m := len(pattern)
182+
half := m >> 1
183+
mask1 := (1 << half) - 1
184+
mask2 := (1 << (m - half)) - 1
185+
for i := 0; i < half; i++ {
186+
a |= pattern[i] << (half - 1 - i)
187+
}
188+
for i := half; i < m; i++ {
189+
b |= pattern[i] << (m - 1 - i)
190+
}
191+
x, y := 0, 0
192+
for i := 1; ; i++ {
193+
v := stream.Next()
194+
y = y<<1 | v
195+
v = (y >> (m - half)) & 1
196+
y &= mask2
197+
x = x<<1 | v
198+
x &= mask1
199+
if i >= m && a == x && b == y {
200+
return i - m
201+
}
202+
}
203+
}
204+
```
205+
206+
<!-- tabs:end -->
207+
208+
<!-- end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /