diff --git a/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README.md b/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README.md index 607077d83f71b..75ccdec366088 100644 --- a/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README.md +++ b/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README.md @@ -43,7 +43,7 @@ tags: [null, false, false, true, false] 解释: -DataStream dataStream = new DataStream(4, 3); // value = 4, k = 3 +DataStream dataStream = new DataStream(4, 3); // value = 4, k = 3 dataStream.consec(4); // 数据流中只有 1 个整数,所以返回 False 。 dataStream.consec(4); // 数据流中只有 2 个整数 // 由于 2 小于 k ,返回 False 。 @@ -70,9 +70,9 @@ dataStream.consec(3); // 最后 k 个整数分别是 [4,4,3] 。 ### 方法一:计数 -维护一个计数器 $cnt$,记录当前连续整数为 `value` 的个数。 +我们可以维护一个计数器 $\textit{cnt}$,记录当前连续整数为 $\textit{value}$ 的个数。 -当 `num` 与 `value` 相等时,$cnt$ 自增 1,否则 $cnt$ 重置为 0。然后判断 $cnt$ 是否大于等于 `k` 即可。 +调用 `consec` 方法时,如果 $\textit{num}$ 与 $\textit{value}$ 相等,我们将 $\textit{cnt}$ 自增 1,否则将 $\textit{cnt}$ 重置为 0。然后判断 $\textit{cnt}$ 是否大于等于 $\textit{k}$ 即可。 时间复杂度 $O(1)$,空间复杂度 $O(1)$。 diff --git a/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README_EN.md b/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README_EN.md index db06510c54582..4810f74a3c607 100644 --- a/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README_EN.md +++ b/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README_EN.md @@ -42,11 +42,11 @@ tags: [null, false, false, true, false] Explanation -DataStream dataStream = new DataStream(4, 3); //value = 4, k = 3 -dataStream.consec(4); // Only 1 integer is parsed, so returns False. +DataStream dataStream = new DataStream(4, 3); //value = 4, k = 3 +dataStream.consec(4); // Only 1 integer is parsed, so returns False. dataStream.consec(4); // Only 2 integers are parsed. - // Since 2 is less than k, returns False. -dataStream.consec(4); // The 3 integers parsed are all equal to value, so returns True. + // Since 2 is less than k, returns False. +dataStream.consec(4); // The 3 integers parsed are all equal to value, so returns True. dataStream.consec(3); // The last k integers parsed in the stream are [4,4,3]. // Since 3 is not equal to value, it returns False. @@ -66,7 +66,13 @@ dataStream.consec(3); // The last k integers parsed in the stream are [4,4,3]. -### Solution 1 +### Solution 1: Counting + +We can maintain a counter $\textit{cnt}$ to record the current number of consecutive integers equal to $\textit{value}$. + +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}$. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. diff --git a/solution/2500-2599/2528.Maximize the Minimum Powered City/README.md b/solution/2500-2599/2528.Maximize the Minimum Powered City/README.md index a063a2f3e728a..f70c3aa6490df 100644 --- a/solution/2500-2599/2528.Maximize the Minimum Powered City/README.md +++ b/solution/2500-2599/2528.Maximize the Minimum Powered City/README.md @@ -95,7 +95,7 @@ tags: 函数 $check(x, k)$ 的实现逻辑是: -遍历每座城市,如果当前城市 $i$ 的供电站数目小于 $x$,此时我们可以贪心地在尽可能右边的位置上建造供电站,位置 $j = min(i + r, n - 1)$,这样可以使得供电站覆盖尽可能多的城市。过程中我们可以借助差分数组,给一段连续的位置加上某个值。如果需要额外建造的供电站数量超过 $k$,那么 $x$ 不满足条件,返回 `false`。否则遍历结束后,返回 `true`。 +遍历每座城市,如果当前城市 $i$ 的供电站数目小于 $x$,此时我们可以贪心地在尽可能右边的位置上建造供电站,位置 $j = \min(i + r, n - 1)$,这样可以使得供电站覆盖尽可能多的城市。过程中我们可以借助差分数组,给一段连续的位置加上某个值。如果需要额外建造的供电站数量超过 $k$,那么 $x$ 不满足条件,返回 `false`。否则遍历结束后,返回 `true`。 时间复杂度 $O(n \times \log M)$,空间复杂度 $O(n)$。其中 $n$ 为城市数量,而 $M$ 我们固定取 $2^{40}$。 diff --git a/solution/2500-2599/2528.Maximize the Minimum Powered City/README_EN.md b/solution/2500-2599/2528.Maximize the Minimum Powered City/README_EN.md index a0ad2f0d95e0a..88b70cc92173b 100644 --- a/solution/2500-2599/2528.Maximize the Minimum Powered City/README_EN.md +++ b/solution/2500-2599/2528.Maximize the Minimum Powered City/README_EN.md @@ -45,8 +45,8 @@ tags:
Input: stations = [1,2,4,5,0], r = 1, k = 2 Output: 5 -Explanation: -One of the optimal ways is to install both the power stations at city 1. +Explanation: +One of the optimal ways is to install both the power stations at city 1. So stations will become [1,4,4,5,0]. - City 0 is provided by 1 + 4 = 5 power stations. - 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.Input: stations = [4,4,4,4], r = 0, k = 3 Output: 4 -Explanation: +Explanation: It can be proved that we cannot make the minimum power of a city greater than 4.@@ -83,7 +83,19 @@ It can be proved that we cannot make the minimum power of a city greater than 4. -### Solution 1 +### Solution 1: Binary Search + Difference Array + Greedy + +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$. + +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. + +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$. + +The implementation logic of the function $check(x, k)$ is as follows: + +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`. + +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}$.