Skip to content

feat: update solutions to lc problems: No.2526,2528 #3722

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
Nov 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ tags:
[null, false, false, true, false]

<strong>解释:</strong>
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 。
Expand All @@ -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)$。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ tags:
[null, false, false, true, false]

<strong>Explanation</strong>
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.
</pre>
Expand All @@ -66,7 +66,13 @@ dataStream.consec(3); // The last k integers parsed in the stream are [4,4,3].

<!-- solution:start -->

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}$。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ tags:
<pre>
<strong>Input:</strong> stations = [1,2,4,5,0], r = 1, k = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong>
One of the optimal ways is to install both the power stations at city 1.
<strong>Explanation:</strong>
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.
Expand All @@ -62,7 +62,7 @@ Since it is not possible to obtain a larger power, we return 5.
<pre>
<strong>Input:</strong> stations = [4,4,4,4], r = 0, k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong>
<strong>Explanation:</strong>
It can be proved that we cannot make the minimum power of a city greater than 4.
</pre>

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

<!-- solution:start -->

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

<!-- tabs:start -->

Expand Down
Loading