Skip to content

Commit 675481a

Browse files
authored
feat: add solutions to lc problem: No.2036 (#1762)
No.2036.Maximum Alternating Subarray Sum
1 parent 39c8496 commit 675481a

File tree

7 files changed

+131
-110
lines changed

7 files changed

+131
-110
lines changed

solution/2000-2099/2036.Maximum Alternating Subarray Sum/README.md

+40-40
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,11 @@
5757

5858
**方法一:动态规划**
5959

60-
定义状态 $a$ 表示以当前元素作为正结尾的最大交替子数组和,状态 $b$ 表示以当前元素作为负结尾的最大交替子数组和。初始时 $a = nums[0]$,$b = -\infty$。
60+
我们定义 $f$ 表示以 $nums[i]$ 结尾的交替子数组的最大和,定义 $g$ 表示以 $-nums[i]$ 结尾的交替子数组的最大和,初始时 $f$ 和 $g$ 均为 $-\infty$。
6161

62-
遍历数组,对于当前元素 $nums[i]$,
62+
接下来,我们遍历数组 $nums$,对于位置 $i$,我们需要维护 $f$ 和 $g$ 的值,即 $f = \max(g, 0) + nums[i]$,而 $g = f - nums[i]$。答案即为所有 $f$ 和 $g$ 中的最大值。
6363

64-
$$
65-
\begin{aligned}
66-
a = \max(nums[i], b + nums[i]) \\
67-
b = a - nums[i]
68-
\end{aligned}
69-
$$
70-
71-
求出 $a$ 和 $b$ 后,将 $a$ 和 $b$ 中的最大值与当前最大交替子数组和进行比较,更新最大交替子数组和。
72-
73-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。
64+
时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
7465

7566
<!-- tabs:start -->
7667

@@ -81,11 +72,10 @@ $$
8172
```python
8273
class Solution:
8374
def maximumAlternatingSubarraySum(self, nums: List[int]) -> int:
84-
ans = nums[0]
85-
a, b = nums[0], -inf
86-
for v in nums[1:]:
87-
a, b = max(v, b + v), a - v
88-
ans = max(ans, a, b)
75+
ans = f = g = -inf
76+
for x in nums:
77+
f, g = max(g, 0) + x, f - x
78+
ans = max(ans, f, g)
8979
return ans
9080
```
9181

@@ -96,13 +86,13 @@ class Solution:
9686
```java
9787
class Solution {
9888
public long maximumAlternatingSubarraySum(int[] nums) {
99-
long ans = nums[0];
100-
long a = nums[0], b = -(1 << 30);
101-
for (int i = 1; i < nums.length; ++i) {
102-
long c = a, d = b;
103-
a = Math.max(nums[i], d + nums[i]);
104-
b = c - nums[i];
105-
ans = Math.max(ans, Math.max(a, b));
89+
final long inf = 1L << 60;
90+
long ans = -inf, f = -inf, g = -inf;
91+
for (int x : nums) {
92+
long ff = Math.max(g, 0) + x;
93+
g = f - x;
94+
f = ff;
95+
ans = Math.max(ans, Math.max(f, g));
10696
}
10797
return ans;
10898
}
@@ -112,18 +102,17 @@ class Solution {
112102
### **C++**
113103

114104
```cpp
115-
using ll = long long;
116-
117105
class Solution {
118106
public:
119107
long long maximumAlternatingSubarraySum(vector<int>& nums) {
120-
ll ans = nums[0];
121-
ll a = nums[0], b = -(1 << 30);
122-
for (int i = 1; i < nums.size(); ++i) {
123-
ll c = a, d = b;
124-
a = max(1ll * nums[i], d + nums[i]);
125-
b = c - nums[i];
126-
ans = max(ans, max(a, b));
108+
using ll = long long;
109+
const ll inf = 1LL << 60;
110+
ll ans = -inf, f = -inf, g = -inf;
111+
for (int x : nums) {
112+
ll ff = max(g, 0LL) + x;
113+
g = f - x;
114+
f = ff;
115+
ans = max({ans, f, g});
127116
}
128117
return ans;
129118
}
@@ -134,13 +123,11 @@ public:
134123
135124
```go
136125
func maximumAlternatingSubarraySum(nums []int) int64 {
137-
ans := nums[0]
138-
a, b := nums[0], -(1 << 30)
139-
for _, v := range nums[1:] {
140-
c, d := a, b
141-
a = max(v, d+v)
142-
b = c - v
143-
ans = max(ans, max(a, b))
126+
const inf = 1 << 60
127+
ans, f, g := -inf, -inf, -inf
128+
for _, x := range nums {
129+
f, g = max(g, 0)+x, f-x
130+
ans = max(ans, max(f, g))
144131
}
145132
return int64(ans)
146133
}
@@ -153,6 +140,19 @@ func max(a, b int) int {
153140
}
154141
```
155142

143+
### **TypeScript**
144+
145+
```ts
146+
function maximumAlternatingSubarraySum(nums: number[]): number {
147+
let [ans, f, g] = [-Infinity, -Infinity, -Infinity];
148+
for (const x of nums) {
149+
[f, g] = [Math.max(g, 0) + x, f - x];
150+
ans = Math.max(ans, f, g);
151+
}
152+
return ans;
153+
}
154+
```
155+
156156
### **...**
157157

158158
```

solution/2000-2099/2036.Maximum Alternating Subarray Sum/README_EN.md

+45-28
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,25 @@ The alternating subarray sum is 1.
5353

5454
## Solutions
5555

56+
**Solution 1: Dynamic Programming**
57+
58+
We define $f$ as the maximum alternating subarray sum ending with $nums[i]$, and $g$ as the maximum alternating subarray sum ending with $-nums[i]$. Initially, $f$ and $g$ are both $-\infty$.
59+
60+
Next, we traverse the array $nums$. For each position $i$, we need to maintain the values of $f$ and $g$, which are $f = \max(g, 0) + nums[i]$ and $g = f - nums[i]$, respectively. The answer is the maximum value among all $f$ and $g$.
61+
62+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
63+
5664
<!-- tabs:start -->
5765

5866
### **Python3**
5967

6068
```python
6169
class Solution:
6270
def maximumAlternatingSubarraySum(self, nums: List[int]) -> int:
63-
ans = nums[0]
64-
a, b = nums[0], -inf
65-
for v in nums[1:]:
66-
a, b = max(v, b + v), a - v
67-
ans = max(ans, a, b)
71+
ans = f = g = -inf
72+
for x in nums:
73+
f, g = max(g, 0) + x, f - x
74+
ans = max(ans, f, g)
6875
return ans
6976
```
7077

@@ -73,13 +80,13 @@ class Solution:
7380
```java
7481
class Solution {
7582
public long maximumAlternatingSubarraySum(int[] nums) {
76-
long ans = nums[0];
77-
long a = nums[0], b = -(1 << 30);
78-
for (int i = 1; i < nums.length; ++i) {
79-
long c = a, d = b;
80-
a = Math.max(nums[i], d + nums[i]);
81-
b = c - nums[i];
82-
ans = Math.max(ans, Math.max(a, b));
83+
final long inf = 1L << 60;
84+
long ans = -inf, f = -inf, g = -inf;
85+
for (int x : nums) {
86+
long ff = Math.max(g, 0) + x;
87+
g = f - x;
88+
f = ff;
89+
ans = Math.max(ans, Math.max(f, g));
8390
}
8491
return ans;
8592
}
@@ -89,18 +96,17 @@ class Solution {
8996
### **C++**
9097

9198
```cpp
92-
using ll = long long;
93-
9499
class Solution {
95100
public:
96101
long long maximumAlternatingSubarraySum(vector<int>& nums) {
97-
ll ans = nums[0];
98-
ll a = nums[0], b = -(1 << 30);
99-
for (int i = 1; i < nums.size(); ++i) {
100-
ll c = a, d = b;
101-
a = max(1ll * nums[i], d + nums[i]);
102-
b = c - nums[i];
103-
ans = max(ans, max(a, b));
102+
using ll = long long;
103+
const ll inf = 1LL << 60;
104+
ll ans = -inf, f = -inf, g = -inf;
105+
for (int x : nums) {
106+
ll ff = max(g, 0LL) + x;
107+
g = f - x;
108+
f = ff;
109+
ans = max({ans, f, g});
104110
}
105111
return ans;
106112
}
@@ -111,13 +117,11 @@ public:
111117
112118
```go
113119
func maximumAlternatingSubarraySum(nums []int) int64 {
114-
ans := nums[0]
115-
a, b := nums[0], -(1 << 30)
116-
for _, v := range nums[1:] {
117-
c, d := a, b
118-
a = max(v, d+v)
119-
b = c - v
120-
ans = max(ans, max(a, b))
120+
const inf = 1 << 60
121+
ans, f, g := -inf, -inf, -inf
122+
for _, x := range nums {
123+
f, g = max(g, 0)+x, f-x
124+
ans = max(ans, max(f, g))
121125
}
122126
return int64(ans)
123127
}
@@ -130,6 +134,19 @@ func max(a, b int) int {
130134
}
131135
```
132136

137+
### **TypeScript**
138+
139+
```ts
140+
function maximumAlternatingSubarraySum(nums: number[]): number {
141+
let [ans, f, g] = [-Infinity, -Infinity, -Infinity];
142+
for (const x of nums) {
143+
[f, g] = [Math.max(g, 0) + x, f - x];
144+
ans = Math.max(ans, f, g);
145+
}
146+
return ans;
147+
}
148+
```
149+
133150
### **...**
134151

135152
```
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
using ll = long long;
2-
3-
class Solution {
4-
public:
5-
long long maximumAlternatingSubarraySum(vector<int>& nums) {
6-
ll ans = nums[0];
7-
ll a = nums[0], b = -(1 << 30);
8-
for (int i = 1; i < nums.size(); ++i) {
9-
ll c = a, d = b;
10-
a = max(1ll * nums[i], d + nums[i]);
11-
b = c - nums[i];
12-
ans = max(ans, max(a, b));
13-
}
14-
return ans;
15-
}
1+
class Solution {
2+
public:
3+
long long maximumAlternatingSubarraySum(vector<int>& nums) {
4+
using ll = long long;
5+
const ll inf = 1LL << 60;
6+
ll ans = -inf, f = -inf, g = -inf;
7+
for (int x : nums) {
8+
ll ff = max(g, 0LL) + x;
9+
g = f - x;
10+
f = ff;
11+
ans = max({ans, f, g});
12+
}
13+
return ans;
14+
}
1615
};

solution/2000-2099/2036.Maximum Alternating Subarray Sum/Solution.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
func maximumAlternatingSubarraySum(nums []int) int64 {
2-
ans := nums[0]
3-
a, b := nums[0], -(1 << 30)
4-
for _, v := range nums[1:] {
5-
c, d := a, b
6-
a = max(v, d+v)
7-
b = c - v
8-
ans = max(ans, max(a, b))
2+
const inf = 1 << 60
3+
ans, f, g := -inf, -inf, -inf
4+
for _, x := range nums {
5+
f, g = max(g, 0)+x, f-x
6+
ans = max(ans, max(f, g))
97
}
108
return int64(ans)
119
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
class Solution {
2-
public long maximumAlternatingSubarraySum(int[] nums) {
3-
long ans = nums[0];
4-
long a = nums[0], b = -(1 << 30);
5-
for (int i = 1; i < nums.length; ++i) {
6-
long c = a, d = b;
7-
a = Math.max(nums[i], d + nums[i]);
8-
b = c - nums[i];
9-
ans = Math.max(ans, Math.max(a, b));
10-
}
11-
return ans;
12-
}
1+
class Solution {
2+
public long maximumAlternatingSubarraySum(int[] nums) {
3+
final long inf = 1L << 60;
4+
long ans = -inf, f = -inf, g = -inf;
5+
for (int x : nums) {
6+
long ff = Math.max(g, 0) + x;
7+
g = f - x;
8+
f = ff;
9+
ans = Math.max(ans, Math.max(f, g));
10+
}
11+
return ans;
12+
}
1313
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
class Solution:
2-
def maximumAlternatingSubarraySum(self, nums: List[int]) -> int:
3-
ans = nums[0]
4-
a, b = nums[0], -inf
5-
for v in nums[1:]:
6-
a, b = max(v, b + v), a - v
7-
ans = max(ans, a, b)
8-
return ans
1+
class Solution:
2+
def maximumAlternatingSubarraySum(self, nums: List[int]) -> int:
3+
ans = f = g = -inf
4+
for x in nums:
5+
f, g = max(g, 0) + x, f - x
6+
ans = max(ans, f, g)
7+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function maximumAlternatingSubarraySum(nums: number[]): number {
2+
let [ans, f, g] = [-Infinity, -Infinity, -Infinity];
3+
for (const x of nums) {
4+
[f, g] = [Math.max(g, 0) + x, f - x];
5+
ans = Math.max(ans, f, g);
6+
}
7+
return ans;
8+
}

0 commit comments

Comments
 (0)