Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2036 #1762

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions solution/2000-2099/2036.Maximum Alternating Subarray Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,11 @@

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

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

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

$$
\begin{aligned}
a = \max(nums[i], b + nums[i]) \\
b = a - nums[i]
\end{aligned}
$$

求出 $a$ 和 $b$ 后,将 $a$ 和 $b$ 中的最大值与当前最大交替子数组和进行比较,更新最大交替子数组和。

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

<!-- tabs:start -->

Expand All @@ -81,11 +72,10 @@ $$
```python
class Solution:
def maximumAlternatingSubarraySum(self, nums: List[int]) -> int:
ans = nums[0]
a, b = nums[0], -inf
for v in nums[1:]:
a, b = max(v, b + v), a - v
ans = max(ans, a, b)
ans = f = g = -inf
for x in nums:
f, g = max(g, 0) + x, f - x
ans = max(ans, f, g)
return ans
```

Expand All @@ -96,13 +86,13 @@ class Solution:
```java
class Solution {
public long maximumAlternatingSubarraySum(int[] nums) {
long ans = nums[0];
long a = nums[0], b = -(1 << 30);
for (int i = 1; i < nums.length; ++i) {
long c = a, d = b;
a = Math.max(nums[i], d + nums[i]);
b = c - nums[i];
ans = Math.max(ans, Math.max(a, b));
final long inf = 1L << 60;
long ans = -inf, f = -inf, g = -inf;
for (int x : nums) {
long ff = Math.max(g, 0) + x;
g = f - x;
f = ff;
ans = Math.max(ans, Math.max(f, g));
}
return ans;
}
Expand All @@ -112,18 +102,17 @@ class Solution {
### **C++**

```cpp
using ll = long long;

class Solution {
public:
long long maximumAlternatingSubarraySum(vector<int>& nums) {
ll ans = nums[0];
ll a = nums[0], b = -(1 << 30);
for (int i = 1; i < nums.size(); ++i) {
ll c = a, d = b;
a = max(1ll * nums[i], d + nums[i]);
b = c - nums[i];
ans = max(ans, max(a, b));
using ll = long long;
const ll inf = 1LL << 60;
ll ans = -inf, f = -inf, g = -inf;
for (int x : nums) {
ll ff = max(g, 0LL) + x;
g = f - x;
f = ff;
ans = max({ans, f, g});
}
return ans;
}
Expand All @@ -134,13 +123,11 @@ public:

```go
func maximumAlternatingSubarraySum(nums []int) int64 {
ans := nums[0]
a, b := nums[0], -(1 << 30)
for _, v := range nums[1:] {
c, d := a, b
a = max(v, d+v)
b = c - v
ans = max(ans, max(a, b))
const inf = 1 << 60
ans, f, g := -inf, -inf, -inf
for _, x := range nums {
f, g = max(g, 0)+x, f-x
ans = max(ans, max(f, g))
}
return int64(ans)
}
Expand All @@ -153,6 +140,19 @@ func max(a, b int) int {
}
```

### **TypeScript**

```ts
function maximumAlternatingSubarraySum(nums: number[]): number {
let [ans, f, g] = [-Infinity, -Infinity, -Infinity];
for (const x of nums) {
[f, g] = [Math.max(g, 0) + x, f - x];
ans = Math.max(ans, f, g);
}
return ans;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,25 @@ The alternating subarray sum is 1.

## Solutions

**Solution 1: Dynamic Programming**

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$.

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$.

The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**

```python
class Solution:
def maximumAlternatingSubarraySum(self, nums: List[int]) -> int:
ans = nums[0]
a, b = nums[0], -inf
for v in nums[1:]:
a, b = max(v, b + v), a - v
ans = max(ans, a, b)
ans = f = g = -inf
for x in nums:
f, g = max(g, 0) + x, f - x
ans = max(ans, f, g)
return ans
```

Expand All @@ -73,13 +80,13 @@ class Solution:
```java
class Solution {
public long maximumAlternatingSubarraySum(int[] nums) {
long ans = nums[0];
long a = nums[0], b = -(1 << 30);
for (int i = 1; i < nums.length; ++i) {
long c = a, d = b;
a = Math.max(nums[i], d + nums[i]);
b = c - nums[i];
ans = Math.max(ans, Math.max(a, b));
final long inf = 1L << 60;
long ans = -inf, f = -inf, g = -inf;
for (int x : nums) {
long ff = Math.max(g, 0) + x;
g = f - x;
f = ff;
ans = Math.max(ans, Math.max(f, g));
}
return ans;
}
Expand All @@ -89,18 +96,17 @@ class Solution {
### **C++**

```cpp
using ll = long long;

class Solution {
public:
long long maximumAlternatingSubarraySum(vector<int>& nums) {
ll ans = nums[0];
ll a = nums[0], b = -(1 << 30);
for (int i = 1; i < nums.size(); ++i) {
ll c = a, d = b;
a = max(1ll * nums[i], d + nums[i]);
b = c - nums[i];
ans = max(ans, max(a, b));
using ll = long long;
const ll inf = 1LL << 60;
ll ans = -inf, f = -inf, g = -inf;
for (int x : nums) {
ll ff = max(g, 0LL) + x;
g = f - x;
f = ff;
ans = max({ans, f, g});
}
return ans;
}
Expand All @@ -111,13 +117,11 @@ public:

```go
func maximumAlternatingSubarraySum(nums []int) int64 {
ans := nums[0]
a, b := nums[0], -(1 << 30)
for _, v := range nums[1:] {
c, d := a, b
a = max(v, d+v)
b = c - v
ans = max(ans, max(a, b))
const inf = 1 << 60
ans, f, g := -inf, -inf, -inf
for _, x := range nums {
f, g = max(g, 0)+x, f-x
ans = max(ans, max(f, g))
}
return int64(ans)
}
Expand All @@ -130,6 +134,19 @@ func max(a, b int) int {
}
```

### **TypeScript**

```ts
function maximumAlternatingSubarraySum(nums: number[]): number {
let [ans, f, g] = [-Infinity, -Infinity, -Infinity];
for (const x of nums) {
[f, g] = [Math.max(g, 0) + x, f - x];
ans = Math.max(ans, f, g);
}
return ans;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using ll = long long;

class Solution {
public:
long long maximumAlternatingSubarraySum(vector<int>& nums) {
ll ans = nums[0];
ll a = nums[0], b = -(1 << 30);
for (int i = 1; i < nums.size(); ++i) {
ll c = a, d = b;
a = max(1ll * nums[i], d + nums[i]);
b = c - nums[i];
ans = max(ans, max(a, b));
}
return ans;
}
class Solution {
public:
long long maximumAlternatingSubarraySum(vector<int>& nums) {
using ll = long long;
const ll inf = 1LL << 60;
ll ans = -inf, f = -inf, g = -inf;
for (int x : nums) {
ll ff = max(g, 0LL) + x;
g = f - x;
f = ff;
ans = max({ans, f, g});
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
func maximumAlternatingSubarraySum(nums []int) int64 {
ans := nums[0]
a, b := nums[0], -(1 << 30)
for _, v := range nums[1:] {
c, d := a, b
a = max(v, d+v)
b = c - v
ans = max(ans, max(a, b))
const inf = 1 << 60
ans, f, g := -inf, -inf, -inf
for _, x := range nums {
f, g = max(g, 0)+x, f-x
ans = max(ans, max(f, g))
}
return int64(ans)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class Solution {
public long maximumAlternatingSubarraySum(int[] nums) {
long ans = nums[0];
long a = nums[0], b = -(1 << 30);
for (int i = 1; i < nums.length; ++i) {
long c = a, d = b;
a = Math.max(nums[i], d + nums[i]);
b = c - nums[i];
ans = Math.max(ans, Math.max(a, b));
}
return ans;
}
class Solution {
public long maximumAlternatingSubarraySum(int[] nums) {
final long inf = 1L << 60;
long ans = -inf, f = -inf, g = -inf;
for (int x : nums) {
long ff = Math.max(g, 0) + x;
g = f - x;
f = ff;
ans = Math.max(ans, Math.max(f, g));
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class Solution:
def maximumAlternatingSubarraySum(self, nums: List[int]) -> int:
ans = nums[0]
a, b = nums[0], -inf
for v in nums[1:]:
a, b = max(v, b + v), a - v
ans = max(ans, a, b)
return ans
class Solution:
def maximumAlternatingSubarraySum(self, nums: List[int]) -> int:
ans = f = g = -inf
for x in nums:
f, g = max(g, 0) + x, f - x
ans = max(ans, f, g)
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function maximumAlternatingSubarraySum(nums: number[]): number {
let [ans, f, g] = [-Infinity, -Infinity, -Infinity];
for (const x of nums) {
[f, g] = [Math.max(g, 0) + x, f - x];
ans = Math.max(ans, f, g);
}
return ans;
}