You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README_EN.md
+11-5Lines changed: 11 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -42,11 +42,11 @@ tags:
42
42
[null, false, false, true, false]
43
43
44
44
<strong>Explanation</strong>
45
-
DataStream dataStream = new DataStream(4, 3); //value = 4, k = 3
46
-
dataStream.consec(4); // Only 1 integer is parsed, so returns False.
45
+
DataStream dataStream = new DataStream(4, 3); //value = 4, k = 3
46
+
dataStream.consec(4); // Only 1 integer is parsed, so returns False.
47
47
dataStream.consec(4); // Only 2 integers are parsed.
48
-
// Since 2 is less than k, returns False.
49
-
dataStream.consec(4); // The 3 integers parsed are all equal to value, so returns True.
48
+
// Since 2 is less than k, returns False.
49
+
dataStream.consec(4); // The 3 integers parsed are all equal to value, so returns True.
50
50
dataStream.consec(3); // The last k integers parsed in the stream are [4,4,3].
51
51
// Since 3 is not equal to value, it returns False.
52
52
</pre>
@@ -66,7 +66,13 @@ dataStream.consec(3); // The last k integers parsed in the stream are [4,4,3].
66
66
67
67
<!-- solution:start -->
68
68
69
-
### Solution 1
69
+
### Solution 1: Counting
70
+
71
+
We can maintain a counter $\textit{cnt}$ to record the current number of consecutive integers equal to $\textit{value}$.
72
+
73
+
When calling the `consec` method, if $\textit{num}$ is equal to $\textit{value}$, we increment $\textit{cnt}$ by 1; otherwise, we reset $\textit{cnt}$ to 0. Then we check whether $\textit{cnt}$ is greater than or equal to $\textit{k}$.
74
+
75
+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
According to the problem description, the minimum number of power stations increases as the value of $k$ increases. Therefore, we can use binary search to find the largest minimum number of power stations, ensuring that the additional power stations needed do not exceed $k$.
89
+
90
+
First, we use a difference array and prefix sum to calculate the initial number of power stations in each city, recording it in the array $s$, where $s[i]$ represents the number of power stations in the $i$-th city.
91
+
92
+
Next, we define the left boundary of the binary search as $0$ and the right boundary as $2^{40}$. Then, we implement a function $check(x, k)$ to determine whether the minimum number of power stations in the cities can be $x$, ensuring that the additional power stations needed do not exceed $k$.
93
+
94
+
The implementation logic of the function $check(x, k)$ is as follows:
95
+
96
+
Traverse each city. If the number of power stations in the current city $i$ is less than $x$, we can greedily build a power station at the rightmost possible position, $j = \min(i + r, n - 1)$, to cover as many cities as possible. During this process, we can use the difference array to add a certain value to a continuous segment. If the number of additional power stations needed exceeds $k$, then $x$ does not meet the condition, and we return `false`. Otherwise, after the traversal, return `true`.
97
+
98
+
The time complexity is $O(n \times \log M)$, and the space complexity is $O(n)$. Here, $n$ is the number of cities, and $M$ is fixed at $2^{40}$.
0 commit comments