Skip to content

Commit dfbb693

Browse files
committed
feat: add solutions to lc problem: No.1031
No.1031.Maximum Sum of Two Non-Overlapping Subarrays
1 parent 6e2b65a commit dfbb693

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

solution/1000-1099/1031.Maximum Sum of Two Non-Overlapping Subarrays/README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@
5959

6060
接下来,我们分两种情况枚举:
6161

62-
假设 $firstLen$ 个元素的子数组在 $secondLen$ 个元素的子数组的左边,那么我们可以枚举 $secondLen$ 个元素的子数组的左端点 $i$,用变量 $t$ 维护左边 $firstLen$ 个元素的子数组的最大和,那么答案就是 $t + s[i + secondLen] - s[i]$。枚举完所有的 $i$,就可以得到候选答案
62+
假设 $firstLen$ 个元素的子数组在 $secondLen$ 个元素的子数组的左边,那么我们可以枚举 $secondLen$ 个元素的子数组的左端点 $i$,用变量 $t$ 维护左边 $firstLen$ 个元素的子数组的最大和,那么当前最大和就是 $t + s[i + secondLen] - s[i]$。其中 $s[i + secondLen] - s[i]$ 表示 $secondLen$ 个元素的子数组的和。枚举完所有的 $i$,就得到了第一种情况下的最大和
6363

64-
假设 $secondLen$ 个元素的子数组在 $firstLen$ 个元素的子数组的左边,那么我们可以枚举 $firstLen$ 个元素的子数组的左端点 $i$,用变量 $t$ 维护左边 $secondLen$ 个元素的子数组的最大和,那么答案就是 $t + s[i + firstLen] - s[i]$。枚举完所有的 $i$,就可以得到候选答案
64+
假设 $secondLen$ 个元素的子数组在 $firstLen$ 个元素的子数组的左边,那么我们可以枚举 $firstLen$ 个元素的子数组的左端点 $i$,用变量 $t$ 维护左边 $secondLen$ 个元素的子数组的最大和,那么当前最大和就是 $t + s[i + firstLen] - s[i]$。其中 $s[i + firstLen] - s[i]$ 表示 $firstLen$ 个元素的子数组的和。枚举完所有的 $i$,就得到了第二种情况下的最大和
6565

66-
最后,我们取两种情况下的候选答案的最大值即可
66+
取两种情况下的最大值作为答案即可
6767

6868
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
6969

@@ -172,6 +172,32 @@ func max(a, b int) int {
172172
}
173173
```
174174

175+
### **TypeScript**
176+
177+
```ts
178+
function maxSumTwoNoOverlap(
179+
nums: number[],
180+
firstLen: number,
181+
secondLen: number,
182+
): number {
183+
const n = nums.length;
184+
const s: number[] = new Array(n + 1).fill(0);
185+
for (let i = 0; i < n; ++i) {
186+
s[i + 1] = s[i] + nums[i];
187+
}
188+
let ans = 0;
189+
for (let i = firstLen, t = 0; i + secondLen - 1 < n; ++i) {
190+
t = Math.max(t, s[i] - s[i - firstLen]);
191+
ans = Math.max(ans, t + s[i + secondLen] - s[i]);
192+
}
193+
for (let i = secondLen, t = 0; i + firstLen - 1 < n; ++i) {
194+
t = Math.max(t, s[i] - s[i - secondLen]);
195+
ans = Math.max(ans, t + s[i + firstLen] - s[i]);
196+
}
197+
return ans;
198+
}
199+
```
200+
175201
### **...**
176202

177203
```

solution/1000-1099/1031.Maximum Sum of Two Non-Overlapping Subarrays/README_EN.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@
4747

4848
## Solutions
4949

50+
**Approach 1: Prefix Sum + Enumeration**
51+
52+
First, we preprocess the prefix sum array $s$ of the array `nums`, where $s[i]$ indicates the sum of the first $i$ elements in `nums`.
53+
54+
Then, we enumerate in two cases:
55+
56+
Suppose $firstLen$ elements of the subarray are on the left side of the $secondLen$ elements of the subarray, then we can enumerate the left endpoint $i$ of the $secondLen$ elements of the subarray, use the variable $t$ to maintain the maximum sum of the left $firstLen$ elements of the subarray, then the current maximum sum is $t + s[i + secondLen] - s[i]$. Where $s[i + secondLen] - s[i]$ represents the sum of the $secondLen$ elements of the subarray. After enumerating all $i$, we get the maximum sum of the first case.
57+
58+
Suppose the $secondLen$ elements of the subarray are on the left side of the $firstLen$ elements of the subarray, then we can enumerate the left endpoint $i$ of the $firstLen$ elements of the subarray, use the variable $t$ to maintain the maximum sum of the left $secondLen$ elements of the subarray, then the current maximum sum is $t + s[i + firstLen] - s[i]$. Where $s[i + firstLen] - s[i]$ represents the sum of the $firstLen$ elements of the subarray. After enumerating all $i$, we get the maximum sum of the second case.
59+
60+
Take the maximum value of the two cases as the answer.
61+
62+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array `nums`.
63+
5064
<!-- tabs:start -->
5165

5266
### **Python3**
@@ -148,6 +162,32 @@ func max(a, b int) int {
148162
}
149163
```
150164

165+
### **TypeScript**
166+
167+
```ts
168+
function maxSumTwoNoOverlap(
169+
nums: number[],
170+
firstLen: number,
171+
secondLen: number,
172+
): number {
173+
const n = nums.length;
174+
const s: number[] = new Array(n + 1).fill(0);
175+
for (let i = 0; i < n; ++i) {
176+
s[i + 1] = s[i] + nums[i];
177+
}
178+
let ans = 0;
179+
for (let i = firstLen, t = 0; i + secondLen - 1 < n; ++i) {
180+
t = Math.max(t, s[i] - s[i - firstLen]);
181+
ans = Math.max(ans, t + s[i + secondLen] - s[i]);
182+
}
183+
for (let i = secondLen, t = 0; i + firstLen - 1 < n; ++i) {
184+
t = Math.max(t, s[i] - s[i - secondLen]);
185+
ans = Math.max(ans, t + s[i + firstLen] - s[i]);
186+
}
187+
return ans;
188+
}
189+
```
190+
151191
### **...**
152192

153193
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function maxSumTwoNoOverlap(
2+
nums: number[],
3+
firstLen: number,
4+
secondLen: number,
5+
): number {
6+
const n = nums.length;
7+
const s: number[] = new Array(n + 1).fill(0);
8+
for (let i = 0; i < n; ++i) {
9+
s[i + 1] = s[i] + nums[i];
10+
}
11+
let ans = 0;
12+
for (let i = firstLen, t = 0; i + secondLen - 1 < n; ++i) {
13+
t = Math.max(t, s[i] - s[i - firstLen]);
14+
ans = Math.max(ans, t + s[i + secondLen] - s[i]);
15+
}
16+
for (let i = secondLen, t = 0; i + firstLen - 1 < n; ++i) {
17+
t = Math.max(t, s[i] - s[i - secondLen]);
18+
ans = Math.max(ans, t + s[i + firstLen] - s[i]);
19+
}
20+
return ans;
21+
}

0 commit comments

Comments
 (0)