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 dc4d031

Browse files
feat: update solutions to lc problems: No.2526,2528 (doocs#3722)
* No.2526.Find Consecutive Integers from a Data Stream * No.2528.Maximize the Minimum Powered City
1 parent 0db8d50 commit dc4d031

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

‎solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ tags:
4343
[null, false, false, true, false]
4444

4545
<strong>解释:</strong>
46-
DataStream dataStream = new DataStream(4, 3); // value = 4, k = 3
46+
DataStream dataStream = new DataStream(4, 3); // value = 4, k = 3
4747
dataStream.consec(4); // 数据流中只有 1 个整数,所以返回 False 。
4848
dataStream.consec(4); // 数据流中只有 2 个整数
4949
// 由于 2 小于 k ,返回 False 。
@@ -70,9 +70,9 @@ dataStream.consec(3); // 最后 k 个整数分别是 [4,4,3] 。
7070

7171
### 方法一:计数
7272

73-
维护一个计数器 $cnt,ドル记录当前连续整数为 `value` 的个数。
73+
我们可以维护一个计数器 $\textit{cnt},ドル记录当前连续整数为 $\textit{value}$ 的个数。
7474

75-
`num``value` 相等时,$cnt$ 自增 1,否则 $cnt$ 重置为 0。然后判断 $cnt$ 是否大于等于 `k` 即可。
75+
调用 `consec` 方法时,如果 $\textit{num}$ 与 $\textit{value}$ 相等,我们将 $\textit{cnt}$ 自增 1,否则将 $\textit{cnt}$ 重置为 0。然后判断 $\textit{cnt}$ 是否大于等于 $\textit{k}$ 即可。
7676

7777
时间复杂度 $O(1),ドル空间复杂度 $O(1)$。
7878

‎solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README_EN.md‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ tags:
4242
[null, false, false, true, false]
4343

4444
<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.
4747
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.
5050
dataStream.consec(3); // The last k integers parsed in the stream are [4,4,3].
5151
// Since 3 is not equal to value, it returns False.
5252
</pre>
@@ -66,7 +66,13 @@ dataStream.consec(3); // The last k integers parsed in the stream are [4,4,3].
6666

6767
<!-- solution:start -->
6868

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

7177
<!-- tabs:start -->
7278

‎solution/2500-2599/2528.Maximize the Minimum Powered City/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ tags:
9595

9696
函数 $check(x, k)$ 的实现逻辑是:
9797

98-
遍历每座城市,如果当前城市 $i$ 的供电站数目小于 $x,ドル此时我们可以贪心地在尽可能右边的位置上建造供电站,位置 $j = min(i + r, n - 1),ドル这样可以使得供电站覆盖尽可能多的城市。过程中我们可以借助差分数组,给一段连续的位置加上某个值。如果需要额外建造的供电站数量超过 $k,ドル那么 $x$ 不满足条件,返回 `false`。否则遍历结束后,返回 `true`
98+
遍历每座城市,如果当前城市 $i$ 的供电站数目小于 $x,ドル此时我们可以贪心地在尽可能右边的位置上建造供电站,位置 $j = \min(i + r, n - 1),ドル这样可以使得供电站覆盖尽可能多的城市。过程中我们可以借助差分数组,给一段连续的位置加上某个值。如果需要额外建造的供电站数量超过 $k,ドル那么 $x$ 不满足条件,返回 `false`。否则遍历结束后,返回 `true`
9999

100100
时间复杂度 $O(n \times \log M),ドル空间复杂度 $O(n)$。其中 $n$ 为城市数量,而 $M$ 我们固定取 2ドル^{40}$。
101101

‎solution/2500-2599/2528.Maximize the Minimum Powered City/README_EN.md‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ tags:
4545
<pre>
4646
<strong>Input:</strong> stations = [1,2,4,5,0], r = 1, k = 2
4747
<strong>Output:</strong> 5
48-
<strong>Explanation:</strong>
49-
One of the optimal ways is to install both the power stations at city 1.
48+
<strong>Explanation:</strong>
49+
One of the optimal ways is to install both the power stations at city 1.
5050
So stations will become [1,4,4,5,0].
5151
- City 0 is provided by 1 + 4 = 5 power stations.
5252
- City 1 is provided by 1 + 4 + 4 = 9 power stations.
@@ -62,7 +62,7 @@ Since it is not possible to obtain a larger power, we return 5.
6262
<pre>
6363
<strong>Input:</strong> stations = [4,4,4,4], r = 0, k = 3
6464
<strong>Output:</strong> 4
65-
<strong>Explanation:</strong>
65+
<strong>Explanation:</strong>
6666
It can be proved that we cannot make the minimum power of a city greater than 4.
6767
</pre>
6868

@@ -83,7 +83,19 @@ It can be proved that we cannot make the minimum power of a city greater than 4.
8383

8484
<!-- solution:start -->
8585

86-
### Solution 1
86+
### Solution 1: Binary Search + Difference Array + Greedy
87+
88+
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}$.
8799

88100
<!-- tabs:start -->
89101

0 commit comments

Comments
(0)

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