Skip to content

feat: add solutions to lc problem: No.3356 #4413

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
May 19, 2025
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
60 changes: 59 additions & 1 deletion solution/3300-3399/3356.Zero Array Transformation II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,21 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:差分数组 + 二分查找

我们注意到,查询的个数越多,越容易使得数组变成零数组,这存在单调性。因此,我们可以二分枚举查询的个数,判断在前 k 个查询下,是否可以将数组变成零数组。

我们定义二分查找的左边界 $l$ 和右边界 $r$,初始时 $l = 0$, $r = m + 1$,其中 $m$ 是查询的个数。我们定义一个函数 $\text{check}(k)$,表示在前 $k$ 个查询下,是否可以将数组变成零数组。我们可以使用差分数组来维护每个元素的值。

定义一个长度为 $n + 1$ 的数组 $d$,初始值全部为 $0$。对于前 $k$ 个查询的每个查询 $[l, r]$,我们将 $d[l]$ 加 $1$,将 $d[r + 1]$ 减 $1$。

然后我们遍历数组 $d$ 在 $[0, n - 1]$ 范围内的每个元素,累加前缀和 $s$,如果 $\textit{nums}[i] > s$,说明 $\textit{nums}$ 不能转换为零数组,返回 $\textit{false}$。

我们在二分查找的过程中,如果 $\text{check}(k)$ 返回 $\text{true}$,说明可以将数组变成零数组,我们就将右边界 $r$ 更新为 $k$,否则将左边界 $l$ 更新为 $k + 1$。

最后,我们判断 $l$ 是否大于 $m$,如果是,则返回 -1,否则返回 $l$。

时间复杂度 $O((n + m) \times \log m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别为数组 $\textit{nums}$ 和 $\textit{queries}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -278,6 +292,50 @@ function minZeroArray(nums: number[], queries: number[][]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn min_zero_array(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> i32 {
let n = nums.len();
let m = queries.len();
let mut d: Vec<i64> = vec![0; n + 1];
let (mut l, mut r) = (0_usize, m + 1);

let check = |k: usize, d: &mut Vec<i64>| -> bool {
d.fill(0);
for i in 0..k {
let (l, r, val) = (
queries[i][0] as usize,
queries[i][1] as usize,
queries[i][2] as i64,
);
d[l] += val;
d[r + 1] -= val;
}
let mut s: i64 = 0;
for i in 0..n {
s += d[i];
if nums[i] as i64 > s {
return false;
}
}
true
};

while l < r {
let mid = (l + r) >> 1;
if check(mid, &mut d) {
r = mid;
} else {
l = mid + 1;
}
}
if l > m { -1 } else { l as i32 }
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,21 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Difference Array + Binary Search

We notice that the more queries we use, the easier it is to turn the array into a zero array, which shows monotonicity. Therefore, we can use binary search to enumerate the number of queries and check whether the array can be turned into a zero array after the first $k$ queries.

We define the left boundary $l$ and right boundary $r$ for binary search, initially $l = 0$, $r = m + 1$, where $m$ is the number of queries. We define a function $\text{check}(k)$ to indicate whether the array can be turned into a zero array after the first $k$ queries. We can use a difference array to maintain the value of each element.

Define an array $d$ of length $n + 1$, initialized to all $0$. For each of the first $k$ queries $[l, r, val]$, we add $val$ to $d[l]$ and subtract $val$ from $d[r + 1]$.

Then we iterate through the array $d$ in the range $[0, n - 1]$, accumulating the prefix sum $s$. If $\textit{nums}[i] > s$, it means $\textit{nums}$ cannot be transformed into a zero array, so we return $\textit{false}$.

During the binary search, if $\text{check}(k)$ returns $\text{true}$, it means the array can be turned into a zero array, so we update the right boundary $r$ to $k$; otherwise, we update the left boundary $l$ to $k + 1$.

Finally, we check whether $l > m$. If so, return -1; otherwise, return $l$.

The time complexity is $O((n + m) \times \log m)$, and the space complexity is $O(n)$, where $n$ and $m$ are the lengths of the array $\textit{nums}$ and $\textit{queries}$, respectively.

<!-- tabs:start -->

Expand Down Expand Up @@ -275,6 +289,50 @@ function minZeroArray(nums: number[], queries: number[][]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn min_zero_array(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> i32 {
let n = nums.len();
let m = queries.len();
let mut d: Vec<i64> = vec![0; n + 1];
let (mut l, mut r) = (0_usize, m + 1);

let check = |k: usize, d: &mut Vec<i64>| -> bool {
d.fill(0);
for i in 0..k {
let (l, r, val) = (
queries[i][0] as usize,
queries[i][1] as usize,
queries[i][2] as i64,
);
d[l] += val;
d[r + 1] -= val;
}
let mut s: i64 = 0;
for i in 0..n {
s += d[i];
if nums[i] as i64 > s {
return false;
}
}
true
};

while l < r {
let mid = (l + r) >> 1;
if check(mid, &mut d) {
r = mid;
} else {
l = mid + 1;
}
}
if l > m { -1 } else { l as i32 }
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
43 changes: 43 additions & 0 deletions solution/3300-3399/3356.Zero Array Transformation II/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
impl Solution {
pub fn min_zero_array(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> i32 {
let n = nums.len();
let m = queries.len();
let mut d: Vec<i64> = vec![0; n + 1];
let (mut l, mut r) = (0_usize, m + 1);

let check = |k: usize, d: &mut Vec<i64>| -> bool {
d.fill(0);
for i in 0..k {
let (l, r, val) = (
queries[i][0] as usize,
queries[i][1] as usize,
queries[i][2] as i64,
);
d[l] += val;
d[r + 1] -= val;
}
let mut s: i64 = 0;
for i in 0..n {
s += d[i];
if nums[i] as i64 > s {
return false;
}
}
true
};

while l < r {
let mid = (l + r) >> 1;
if check(mid, &mut d) {
r = mid;
} else {
l = mid + 1;
}
}
if l > m {
-1
} else {
l as i32
}
}
}