Skip to content

Commit a76908b

Browse files
authored
feat: add solutions to lc problem: No.0325 (#4281)
No.0325.Maximum Size Subarray Sum Equals k
1 parent 9ce6b20 commit a76908b

File tree

5 files changed

+221
-6
lines changed

5 files changed

+221
-6
lines changed

solution/0300-0399/0325.Maximum Size Subarray Sum Equals k/README.md

+79-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tags:
2626

2727
<pre>
2828
<strong>输入: </strong><em>nums</em> = <code>[1,-1,5,-2,3]</code>, <em>k</em> = <code>3</code>
29-
<strong>输出: </strong>4
29+
<strong>输出: </strong>4
3030
<strong>解释: </strong>子数组 <code>[1, -1, 5, -2]</code> 和等于 3,且长度最长。
3131
</pre>
3232

@@ -55,13 +55,13 @@ tags:
5555

5656
### 方法一:哈希表 + 前缀和
5757

58-
我们可以用一个哈希表 $d$ 记录数组 $nums$ 中每个前缀和第一次出现的下标,初始时 $d[0] = -1$。另外定义一个变量 $s$ 记录前缀和。
58+
我们可以用一个哈希表 $\textit{d}$ 记录数组 $\textit{nums}$ 中每个前缀和第一次出现的下标,初始时 $\textit{d}[0] = -1$。另外定义一个变量 $\textit{s}$ 记录前缀和。
5959

60-
接下来,遍历数组 $nums$,对于当前遍历到的数字 $nums[i]$,我们更新前缀和 $s = s + nums[i]$,如果 $s-k$ 在哈希表 $d$ 中存在,不妨记 $j = d[s - k]$,那么以 $nums[i]$ 结尾的符合条件的子数组的长度为 $i - j$,我们使用一个变量 $ans$ 来维护最长的符合条件的子数组的长度。然后,如果 $s$ 在哈希表中不存在,我们记录 $s$ 和对应的下标 $i$,即 $d[s] = i$,否则我们不更新 $d[s]$。需要注意的是,可能会有多个位置 $i$ 都满足 $s$ 的值,因此我们只记录最小的 $i$,这样就能保证子数组的长度最长。
60+
接下来,遍历数组 $\textit{nums}$,对于当前遍历到的数字 $\textit{nums}[i]$,我们更新前缀和 $\textit{s} = \textit{s} + \textit{nums}[i]$,如果 $\textit{s}-k$ 在哈希表 $\textit{d}$ 中存在,不妨记 $j = \textit{d}[\textit{s} - k]$,那么以 $\textit{nums}[i]$ 结尾的符合条件的子数组的长度为 $i - j$,我们使用一个变量 $\textit{ans}$ 来维护最长的符合条件的子数组的长度。然后,如果 $\textit{s}$ 在哈希表中不存在,我们记录 $\textit{s}$ 和对应的下标 $i$,即 $\textit{d}[\textit{s}] = i$,否则我们不更新 $\textit{d}[\textit{s}]$。需要注意的是,可能会有多个位置 $i$ 都满足 $\textit{s}$ 的值,因此我们只记录最小的 $i$,这样就能保证子数组的长度最长。
6161

62-
遍历结束之后,我们返回 $ans$ 即可。
62+
遍历结束之后,我们返回 $\textit{ans}$ 即可。
6363

64-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。
64+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
6565

6666
<!-- tabs:start -->
6767

@@ -163,6 +163,80 @@ function maxSubArrayLen(nums: number[], k: number): number {
163163
}
164164
```
165165

166+
#### Rust
167+
168+
```rust
169+
use std::collections::HashMap;
170+
171+
impl Solution {
172+
pub fn max_sub_array_len(nums: Vec<i32>, k: i32) -> i32 {
173+
let mut d = HashMap::new();
174+
d.insert(0, -1);
175+
let mut ans = 0;
176+
let mut s = 0;
177+
178+
for (i, &x) in nums.iter().enumerate() {
179+
s += x;
180+
if let Some(&j) = d.get(&(s - k)) {
181+
ans = ans.max((i as i32) - j);
182+
}
183+
d.entry(s).or_insert(i as i32);
184+
}
185+
186+
ans
187+
}
188+
}
189+
```
190+
191+
#### JavaScript
192+
193+
```js
194+
/**
195+
* @param {number[]} nums
196+
* @param {number} k
197+
* @return {number}
198+
*/
199+
var maxSubArrayLen = function (nums, k) {
200+
const d = new Map();
201+
d.set(0, -1);
202+
let ans = 0;
203+
let s = 0;
204+
for (let i = 0; i < nums.length; ++i) {
205+
s += nums[i];
206+
if (d.has(s - k)) {
207+
ans = Math.max(ans, i - d.get(s - k));
208+
}
209+
if (!d.has(s)) {
210+
d.set(s, i);
211+
}
212+
}
213+
return ans;
214+
};
215+
```
216+
217+
#### C#
218+
219+
```cs
220+
public class Solution {
221+
public int MaxSubArrayLen(int[] nums, int k) {
222+
var d = new Dictionary<int, int>();
223+
d[0] = -1;
224+
int ans = 0;
225+
int s = 0;
226+
for (int i = 0; i < nums.Length; i++) {
227+
s += nums[i];
228+
if (d.ContainsKey(s - k)) {
229+
ans = Math.Max(ans, i - d[s - k]);
230+
}
231+
if (!d.ContainsKey(s)) {
232+
d[s] = i;
233+
}
234+
}
235+
return ans;
236+
}
237+
}
238+
```
239+
166240
<!-- tabs:end -->
167241

168242
<!-- solution:end -->

solution/0300-0399/0325.Maximum Size Subarray Sum Equals k/README_EN.md

+83-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ tags:
5252

5353
<!-- solution:start -->
5454

55-
### Solution 1
55+
### Solution 1: Hash Table + Prefix Sum
56+
57+
We can use a hash table $\textit{d}$ to record the first occurrence index of each prefix sum in the array $\textit{nums}$, initializing $\textit{d}[0] = -1$. Additionally, we define a variable $\textit{s}$ to keep track of the current prefix sum.
58+
59+
Next, we iterate through the array $\textit{nums}$. For the current number $\textit{nums}[i]$, we update the prefix sum $\textit{s} = \textit{s} + \textit{nums}[i]$. If $\textit{s} - k$ exists in the hash table $\textit{d}$, let $\textit{j} = \textit{d}[\textit{s} - k]$, then the length of the subarray that ends at $\textit{nums}[i]$ and satisfies the condition is $i - j$. We use a variable $\textit{ans}$ to maintain the length of the longest subarray that satisfies the condition. After that, if $\textit{s}$ does not exist in the hash table, we record $\textit{s}$ and its corresponding index $i$ by setting $\textit{d}[\textit{s}] = i$. Otherwise, we do not update $\textit{d}[\textit{s}]$. It is important to note that there may be multiple positions $i$ with the same value of $\textit{s}$, so we only record the smallest $i$ to ensure the subarray length is the longest.
60+
61+
After the iteration ends, we return $\textit{ans}$.
62+
63+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$.
5664

5765
<!-- tabs:start -->
5866

@@ -154,6 +162,80 @@ function maxSubArrayLen(nums: number[], k: number): number {
154162
}
155163
```
156164

165+
#### Rust
166+
167+
```rust
168+
use std::collections::HashMap;
169+
170+
impl Solution {
171+
pub fn max_sub_array_len(nums: Vec<i32>, k: i32) -> i32 {
172+
let mut d = HashMap::new();
173+
d.insert(0, -1);
174+
let mut ans = 0;
175+
let mut s = 0;
176+
177+
for (i, &x) in nums.iter().enumerate() {
178+
s += x;
179+
if let Some(&j) = d.get(&(s - k)) {
180+
ans = ans.max((i as i32) - j);
181+
}
182+
d.entry(s).or_insert(i as i32);
183+
}
184+
185+
ans
186+
}
187+
}
188+
```
189+
190+
#### JavaScript
191+
192+
```js
193+
/**
194+
* @param {number[]} nums
195+
* @param {number} k
196+
* @return {number}
197+
*/
198+
var maxSubArrayLen = function (nums, k) {
199+
const d = new Map();
200+
d.set(0, -1);
201+
let ans = 0;
202+
let s = 0;
203+
for (let i = 0; i < nums.length; ++i) {
204+
s += nums[i];
205+
if (d.has(s - k)) {
206+
ans = Math.max(ans, i - d.get(s - k));
207+
}
208+
if (!d.has(s)) {
209+
d.set(s, i);
210+
}
211+
}
212+
return ans;
213+
};
214+
```
215+
216+
#### C#
217+
218+
```cs
219+
public class Solution {
220+
public int MaxSubArrayLen(int[] nums, int k) {
221+
var d = new Dictionary<int, int>();
222+
d[0] = -1;
223+
int ans = 0;
224+
int s = 0;
225+
for (int i = 0; i < nums.Length; i++) {
226+
s += nums[i];
227+
if (d.ContainsKey(s - k)) {
228+
ans = Math.Max(ans, i - d[s - k]);
229+
}
230+
if (!d.ContainsKey(s)) {
231+
d[s] = i;
232+
}
233+
}
234+
return ans;
235+
}
236+
}
237+
```
238+
157239
<!-- tabs:end -->
158240

159241
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public int MaxSubArrayLen(int[] nums, int k) {
3+
var d = new Dictionary<int, int>();
4+
d[0] = -1;
5+
int ans = 0;
6+
int s = 0;
7+
for (int i = 0; i < nums.Length; i++) {
8+
s += nums[i];
9+
if (d.ContainsKey(s - k)) {
10+
ans = Math.Max(ans, i - d[s - k]);
11+
}
12+
if (!d.ContainsKey(s)) {
13+
d[s] = i;
14+
}
15+
}
16+
return ans;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var maxSubArrayLen = function (nums, k) {
7+
const d = new Map();
8+
d.set(0, -1);
9+
let ans = 0;
10+
let s = 0;
11+
for (let i = 0; i < nums.length; ++i) {
12+
s += nums[i];
13+
if (d.has(s - k)) {
14+
ans = Math.max(ans, i - d.get(s - k));
15+
}
16+
if (!d.has(s)) {
17+
d.set(s, i);
18+
}
19+
}
20+
return ans;
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn max_sub_array_len(nums: Vec<i32>, k: i32) -> i32 {
5+
let mut d = HashMap::new();
6+
d.insert(0, -1);
7+
let mut ans = 0;
8+
let mut s = 0;
9+
10+
for (i, &x) in nums.iter().enumerate() {
11+
s += x;
12+
if let Some(&j) = d.get(&(s - k)) {
13+
ans = ans.max((i as i32) - j);
14+
}
15+
d.entry(s).or_insert(i as i32);
16+
}
17+
18+
ans
19+
}
20+
}

0 commit comments

Comments
 (0)