Skip to content
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

feat: add solutions to lc problem: No.1524 #4112

Merged
merged 1 commit into from
Feb 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ tags:

### 方法一:前缀和 + 计数器

我们定义一个长度为 $2$ 的数组 $cnt$ 作为计数器,其中 $cnt[0]$ 和 $cnt[1]$ 分别表示前缀和为偶数和奇数的子数组的个数。初始时 $cnt[0] = 1$,而 $cnt[1] = 0$。
我们定义一个长度为 $2$ 的数组 $\textit{cnt}$ 作为计数器,其中 $\textit{cnt}[0]$ 和 $\textit{cnt}[1]$ 分别表示前缀和为偶数和奇数的子数组的个数。初始时 $\textit{cnt}[0] = 1$,而 $\textit{cnt}[1] = 0$。

接下来,我们维护当前的前缀和 $s$,初始时 $s = 0$。

遍历数组 $arr$,对于遍历到的每个元素 $x$,我们将 $s$ 的值加上 $x$,然后根据 $s$ 的奇偶性,将 $cnt[s \mod 2 \oplus 1]$ 的值累加到答案中,然后我们将 $cnt[s \mod 2]$ 的值加 $1$。
遍历数组 $\textit{arr}$,对于遍历到的每个元素 $x$,我们将 $s$ 的值加上 $x$,然后根据 $s$ 的奇偶性,将 $\textit{cnt}[s \mod 2 \oplus 1]$ 的值累加到答案中,然后我们将 $\textit{cnt}[s \mod 2]$ 的值加 $1$。

遍历结束后,我们即可得到答案。注意答案的取模运算。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $arr$ 的长度。
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{arr}$ 的长度。空间复杂度 $O(1)$

<!-- tabs:start -->

Expand Down Expand Up @@ -177,6 +177,25 @@ function numOfSubarrays(arr: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn num_of_subarrays(arr: Vec<i32>) -> i32 {
const MOD: i32 = 1_000_000_007;
let mut cnt = [1, 0];
let mut ans = 0;
let mut s = 0;
for &x in arr.iter() {
s += x;
ans = (ans + cnt[((s & 1) ^ 1) as usize]) % MOD;
cnt[(s & 1) as usize] += 1;
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,17 @@ All sub-arrays have even sum and the answer is 0.

<!-- solution:start -->

### Solution 1
### Solution 1: Prefix Sum + Counter

We define an array $\textit{cnt}$ of length 2 as a counter, where $\textit{cnt}[0]$ and $\textit{cnt}[1]$ represent the number of subarrays with even and odd prefix sums, respectively. Initially, $\textit{cnt}[0] = 1$ and $\textit{cnt}[1] = 0$.

Next, we maintain the current prefix sum $s$, initially $s = 0$.

Traverse the array $\textit{arr}$, for each element $x$ encountered, add the value of $x$ to $s$, then based on the parity of $s$, add the value of $\textit{cnt}[s \mod 2 \oplus 1]$ to the answer, and then increment the value of $\textit{cnt}[s \mod 2]$ by 1.

After the traversal, we get the answer. Note the modulo operation for the answer.

Time complexity is $O(n)$, where $n$ is the length of the array $\textit{arr}$. Space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -156,6 +166,25 @@ function numOfSubarrays(arr: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn num_of_subarrays(arr: Vec<i32>) -> i32 {
const MOD: i32 = 1_000_000_007;
let mut cnt = [1, 0];
let mut ans = 0;
let mut s = 0;
for &x in arr.iter() {
s += x;
ans = (ans + cnt[((s & 1) ^ 1) as usize]) % MOD;
cnt[(s & 1) as usize] += 1;
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
impl Solution {
pub fn num_of_subarrays(arr: Vec<i32>) -> i32 {
const MOD: i32 = 1_000_000_007;
let mut cnt = [1, 0];
let mut ans = 0;
let mut s = 0;
for &x in arr.iter() {
s += x;
ans = (ans + cnt[((s & 1) ^ 1) as usize]) % MOD;
cnt[(s & 1) as usize] += 1;
}
ans
}
}