|
| 1 | +# [2036. Maximum Alternating Subarray Sum](https://leetcode-cn.com/problems/maximum-alternating-subarray-sum) |
| 2 | + |
| 3 | +[English Version](/solution/2000-2099/2036.Maximum%20Alternating%20Subarray%20Sum/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>A <strong>subarray</strong> of a <strong>0-indexed</strong> integer array is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> |
| 10 | + |
| 11 | +<p>The <strong>alternating subarray sum</strong> of a subarray that ranges from index <code>i</code> to <code>j</code> (<strong>inclusive</strong>, <code>0 <= i <= j < nums.length</code>) is <code>nums[i] - nums[i+1] + nums[i+2] - ... +/- nums[j]</code>.</p> |
| 12 | + |
| 13 | +<p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, return <em>the <strong>maximum alternating subarray sum</strong> of any subarray of </em><code>nums</code>.</p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | +<p><strong>Example 1:</strong></p> |
| 17 | + |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> nums = [3,-1,1,2] |
| 20 | +<strong>Output:</strong> 5 |
| 21 | +<strong>Explanation:</strong> |
| 22 | +The subarray [3,-1,1] has the largest alternating subarray sum. |
| 23 | +The alternating subarray sum is 3 - (-1) + 1 = 5. |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p><strong>Example 2:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input:</strong> nums = [2,2,2,2,2] |
| 30 | +<strong>Output:</strong> 2 |
| 31 | +<strong>Explanation:</strong> |
| 32 | +The subarrays [2], [2,2,2], and [2,2,2,2,2] have the largest alternating subarray sum. |
| 33 | +The alternating subarray sum of [2] is 2. |
| 34 | +The alternating subarray sum of [2,2,2] is 2 - 2 + 2 = 2. |
| 35 | +The alternating subarray sum of [2,2,2,2,2] is 2 - 2 + 2 - 2 + 2 = 2. |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p><strong>Example 3:</strong></p> |
| 39 | + |
| 40 | +<pre> |
| 41 | +<strong>Input:</strong> nums = [1] |
| 42 | +<strong>Output:</strong> 1 |
| 43 | +<strong>Explanation:</strong> |
| 44 | +There is only one non-empty subarray, which is [1]. |
| 45 | +The alternating subarray sum is 1. |
| 46 | +</pre> |
| 47 | + |
| 48 | +<p> </p> |
| 49 | +<p><strong>Constraints:</strong></p> |
| 50 | + |
| 51 | +<ul> |
| 52 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 53 | + <li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li> |
| 54 | +</ul> |
| 55 | + |
| 56 | + |
| 57 | +## 解法 |
| 58 | + |
| 59 | +<!-- 这里可写通用的实现逻辑 --> |
| 60 | + |
| 61 | +<!-- tabs:start --> |
| 62 | + |
| 63 | +### **Python3** |
| 64 | + |
| 65 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 66 | + |
| 67 | +```python |
| 68 | + |
| 69 | +``` |
| 70 | + |
| 71 | +### **Java** |
| 72 | + |
| 73 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 74 | + |
| 75 | +```java |
| 76 | + |
| 77 | +``` |
| 78 | + |
| 79 | +### **...** |
| 80 | + |
| 81 | +``` |
| 82 | +
|
| 83 | +``` |
| 84 | + |
| 85 | +<!-- tabs:end --> |
0 commit comments