Skip to content

Commit aa8b52a

Browse files
authored
feat: add solutions to lc problem: No.3381 (#4853)
1 parent 026ac4e commit aa8b52a

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,17 @@ tags:
7878

7979
<!-- solution:start -->
8080

81-
### 方法一
81+
### 方法一:前缀和 + 枚举
82+
83+
根据题目描述,要使得子数组的长度可以被 $k$ 整除,等价于要求子数组 $\textit{nums}[i+1 \ldots j]$ 中,满足 $i \bmod k = j \bmod k$。
84+
85+
我们可以枚举子数组的右端点 $j$,并使用一个长度为 $k$ 的数组 $\textit{f}$ 来记录每个模 $k$ 的前缀和的最小值。初始时 $\textit{f}[k-1] = 0$,表示下标 $-1$ 的前缀和为 $0$。
86+
87+
那么对于当前的右端点 $j$,前缀和为 $s$,我们可以计算出以 $j$ 为右端点的、长度可以被 $k$ 整除的子数组的最大和为 $s - \textit{f}[j \bmod k]$,以此更新答案。同时,我们也需要更新 $\textit{f}[j \bmod k]$,使其等于当前前缀和 $s$ 和 $\textit{f}[j \bmod k]$ 的较小值。
88+
89+
枚举结束后,返回答案即可。
90+
91+
时间复杂度 $O(n)$,空间复杂度 $O(k)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
8292

8393
<!-- tabs:start -->
8494

@@ -180,6 +190,27 @@ function maxSubarraySum(nums: number[], k: number): number {
180190
}
181191
```
182192

193+
#### Rust
194+
195+
```rust
196+
impl Solution {
197+
pub fn max_subarray_sum(nums: Vec<i32>, k: i32) -> i64 {
198+
let k = k as usize;
199+
let inf = 1i64 << 62;
200+
let mut f = vec![inf; k];
201+
f[k - 1] = 0;
202+
let mut s = 0i64;
203+
let mut ans = -inf;
204+
for (i, &x) in nums.iter().enumerate() {
205+
s += x as i64;
206+
ans = ans.max(s - f[i % k]);
207+
f[i % k] = f[i % k].min(s);
208+
}
209+
ans
210+
}
211+
}
212+
```
213+
183214
<!-- tabs:end -->
184215

185216
<!-- solution:end -->

solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README_EN.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,17 @@ tags:
7575

7676
<!-- solution:start -->
7777

78-
### Solution 1
78+
### Solution 1: Prefix Sum + Enumeration
79+
80+
According to the problem description, for a subarray's length to be divisible by $k$, it is equivalent to requiring that for subarray $\textit{nums}[i+1 \ldots j]$, we have $i \bmod k = j \bmod k$.
81+
82+
We can enumerate the right endpoint $j$ of the subarray and use an array $\textit{f}$ of length $k$ to record the minimum prefix sum for each modulo $k$. Initially, $\textit{f}[k-1] = 0$, indicating that the prefix sum at index $-1$ is $0$.
83+
84+
Then for the current right endpoint $j$ with prefix sum $s$, we can calculate the maximum sum of subarrays ending at $j$ with length divisible by $k$ as $s - \textit{f}[j \bmod k]$, and update the answer accordingly. At the same time, we need to update $\textit{f}[j \bmod k]$ to be the minimum of the current prefix sum $s$ and $\textit{f}[j \bmod k]$.
85+
86+
After the enumeration is complete, return the answer.
87+
88+
The time complexity is $O(n)$ and the space complexity is $O(k)$, where $n$ is the length of the array $\textit{nums}$.
7989

8090
<!-- tabs:start -->
8191

@@ -177,6 +187,27 @@ function maxSubarraySum(nums: number[], k: number): number {
177187
}
178188
```
179189

190+
#### Rust
191+
192+
```rust
193+
impl Solution {
194+
pub fn max_subarray_sum(nums: Vec<i32>, k: i32) -> i64 {
195+
let k = k as usize;
196+
let inf = 1i64 << 62;
197+
let mut f = vec![inf; k];
198+
f[k - 1] = 0;
199+
let mut s = 0i64;
200+
let mut ans = -inf;
201+
for (i, &x) in nums.iter().enumerate() {
202+
s += x as i64;
203+
ans = ans.max(s - f[i % k]);
204+
f[i % k] = f[i % k].min(s);
205+
}
206+
ans
207+
}
208+
}
209+
```
210+
180211
<!-- tabs:end -->
181212

182213
<!-- solution:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn max_subarray_sum(nums: Vec<i32>, k: i32) -> i64 {
3+
let k = k as usize;
4+
let inf = 1i64 << 62;
5+
let mut f = vec![inf; k];
6+
f[k - 1] = 0;
7+
let mut s = 0i64;
8+
let mut ans = -inf;
9+
for (i, &x) in nums.iter().enumerate() {
10+
s += x as i64;
11+
ans = ans.max(s - f[i % k]);
12+
f[i % k] = f[i % k].min(s);
13+
}
14+
ans
15+
}
16+
}

0 commit comments

Comments
 (0)