diff --git a/solution/3300-3399/3356.Zero Array Transformation II/README.md b/solution/3300-3399/3356.Zero Array Transformation II/README.md index 08b2fe35d833d..fa2ec9a5e2b13 100644 --- a/solution/3300-3399/3356.Zero Array Transformation II/README.md +++ b/solution/3300-3399/3356.Zero Array Transformation II/README.md @@ -109,7 +109,21 @@ tags: -### 方法一 +### 方法一:差分数组 + 二分查找 + +我们注意到,查询的个数越多,越容易使得数组变成零数组,这存在单调性。因此,我们可以二分枚举查询的个数,判断在前 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}$ 的长度。 @@ -278,6 +292,50 @@ function minZeroArray(nums: number[], queries: number[][]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_zero_array(nums: Vec, queries: Vec>) -> i32 { + let n = nums.len(); + let m = queries.len(); + let mut d: Vec = vec![0; n + 1]; + let (mut l, mut r) = (0_usize, m + 1); + + let check = |k: usize, d: &mut Vec| -> 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 } + } +} +``` + diff --git a/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md b/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md index 122f68ede8c09..0d06addbad36d 100644 --- a/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md +++ b/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md @@ -106,7 +106,21 @@ tags: -### 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. @@ -275,6 +289,50 @@ function minZeroArray(nums: number[], queries: number[][]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_zero_array(nums: Vec, queries: Vec>) -> i32 { + let n = nums.len(); + let m = queries.len(); + let mut d: Vec = vec![0; n + 1]; + let (mut l, mut r) = (0_usize, m + 1); + + let check = |k: usize, d: &mut Vec| -> 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 } + } +} +``` + diff --git a/solution/3300-3399/3356.Zero Array Transformation II/Solution.rs b/solution/3300-3399/3356.Zero Array Transformation II/Solution.rs new file mode 100644 index 0000000000000..17963bcad041d --- /dev/null +++ b/solution/3300-3399/3356.Zero Array Transformation II/Solution.rs @@ -0,0 +1,43 @@ +impl Solution { + pub fn min_zero_array(nums: Vec, queries: Vec>) -> i32 { + let n = nums.len(); + let m = queries.len(); + let mut d: Vec = vec![0; n + 1]; + let (mut l, mut r) = (0_usize, m + 1); + + let check = |k: usize, d: &mut Vec| -> 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 + } + } +}