From 2ad4822d4a11ada57f8fb36c91fb93ce53d0246e Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 20 Nov 2025 21:56:58 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3381 --- .../README.md | 33 ++++++++++++++++++- .../README_EN.md | 33 ++++++++++++++++++- .../Solution.rs | 16 +++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.rs diff --git a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README.md b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README.md index 7f1769aaeba07..78bb421b39a72 100644 --- a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README.md +++ b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README.md @@ -78,7 +78,17 @@ tags: -### 方法一 +### 方法一:前缀和 + 枚举 + +根据题目描述,要使得子数组的长度可以被 $k$ 整除,等价于要求子数组 $\textit{nums}[i+1 \ldots j]$ 中,满足 $i \bmod k = j \bmod k$。 + +我们可以枚举子数组的右端点 $j$,并使用一个长度为 $k$ 的数组 $\textit{f}$ 来记录每个模 $k$ 的前缀和的最小值。初始时 $\textit{f}[k-1] = 0$,表示下标 $-1$ 的前缀和为 $0$。 + +那么对于当前的右端点 $j$,前缀和为 $s$,我们可以计算出以 $j$ 为右端点的、长度可以被 $k$ 整除的子数组的最大和为 $s - \textit{f}[j \bmod k]$,以此更新答案。同时,我们也需要更新 $\textit{f}[j \bmod k]$,使其等于当前前缀和 $s$ 和 $\textit{f}[j \bmod k]$ 的较小值。 + +枚举结束后,返回答案即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(k)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 @@ -180,6 +190,27 @@ function maxSubarraySum(nums: number[], k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn max_subarray_sum(nums: Vec, k: i32) -> i64 { + let k = k as usize; + let inf = 1i64 << 62; + let mut f = vec![inf; k]; + f[k - 1] = 0; + let mut s = 0i64; + let mut ans = -inf; + for (i, &x) in nums.iter().enumerate() { + s += x as i64; + ans = ans.max(s - f[i % k]); + f[i % k] = f[i % k].min(s); + } + ans + } +} +``` + diff --git a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README_EN.md b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README_EN.md index 6940c95156067..e1fd64565386a 100644 --- a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README_EN.md +++ b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README_EN.md @@ -75,7 +75,17 @@ tags: -### Solution 1 +### Solution 1: Prefix Sum + Enumeration + +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$. + +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$. + +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]$. + +After the enumeration is complete, return the answer. + +The time complexity is $O(n)$ and the space complexity is $O(k)$, where $n$ is the length of the array $\textit{nums}$. @@ -177,6 +187,27 @@ function maxSubarraySum(nums: number[], k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn max_subarray_sum(nums: Vec, k: i32) -> i64 { + let k = k as usize; + let inf = 1i64 << 62; + let mut f = vec![inf; k]; + f[k - 1] = 0; + let mut s = 0i64; + let mut ans = -inf; + for (i, &x) in nums.iter().enumerate() { + s += x as i64; + ans = ans.max(s - f[i % k]); + f[i % k] = f[i % k].min(s); + } + ans + } +} +``` + diff --git a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.rs b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.rs new file mode 100644 index 0000000000000..ccd0213dc62fd --- /dev/null +++ b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.rs @@ -0,0 +1,16 @@ +impl Solution { + pub fn max_subarray_sum(nums: Vec, k: i32) -> i64 { + let k = k as usize; + let inf = 1i64 << 62; + let mut f = vec![inf; k]; + f[k - 1] = 0; + let mut s = 0i64; + let mut ans = -inf; + for (i, &x) in nums.iter().enumerate() { + s += x as i64; + ans = ans.max(s - f[i % k]); + f[i % k] = f[i % k].min(s); + } + ans + } +}