|
| 1 | +# [2219. Maximum Sum Score of Array](https://leetcode-cn.com/problems/maximum-sum-score-of-array) |
| 2 | + |
| 3 | +[English Version](/solution/2200-2299/2219.Maximum%20Sum%20Score%20of%20Array/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p> |
| 10 | + |
| 11 | +<p>The <strong>sum </strong><strong>score</strong> of <code>nums</code> at an index <code>i</code> where <code>0 <= i < n</code> is the <strong>maximum</strong> of:</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li>The sum of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code>.</li> |
| 15 | + <li>The sum of the <strong>last</strong> <code>n - i</code> elements of <code>nums</code>.</li> |
| 16 | +</ul> |
| 17 | + |
| 18 | +<p>Return <em>the <strong>maximum</strong> <strong>sum </strong><strong>score</strong> of </em><code>nums</code><em> at any index.</em></p> |
| 19 | + |
| 20 | +<p> </p> |
| 21 | +<p><strong>Example 1:</strong></p> |
| 22 | + |
| 23 | +<pre> |
| 24 | +<strong>Input:</strong> nums = [4,3,-2,5] |
| 25 | +<strong>Output:</strong> 10 |
| 26 | +<strong>Explanation:</strong> |
| 27 | +The sum score at index 0 is max(4, 4 + 3 + -2 + 5) = max(4, 10) = 10. |
| 28 | +The sum score at index 1 is max(4 + 3, 3 + -2 + 5) = max(7, 6) = 7. |
| 29 | +The sum score at index 2 is max(4 + 3 + -2, -2 + 5) = max(5, 3) = 5. |
| 30 | +The sum score at index 3 is max(4 + 3 + -2 + 5, 5) = max(10, 5) = 10. |
| 31 | +The maximum sum score of nums is 10. |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p><strong>Example 2:</strong></p> |
| 35 | + |
| 36 | +<pre> |
| 37 | +<strong>Input:</strong> nums = [-3,-5] |
| 38 | +<strong>Output:</strong> -3 |
| 39 | +<strong>Explanation:</strong> |
| 40 | +The sum score at index 0 is max(-3, -3 + -5) = max(-3, -8) = -3. |
| 41 | +The sum score at index 1 is max(-3 + -5, -5) = max(-8, -5) = -5. |
| 42 | +The maximum sum score of nums is -3. |
| 43 | +</pre> |
| 44 | + |
| 45 | +<p> </p> |
| 46 | +<p><strong>Constraints:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li><code>n == nums.length</code></li> |
| 50 | + <li><code>1 <= n <= 10<sup>5</sup></code></li> |
| 51 | + <li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li> |
| 52 | +</ul> |
| 53 | + |
| 54 | +## 解法 |
| 55 | + |
| 56 | +<!-- 这里可写通用的实现逻辑 --> |
| 57 | + |
| 58 | +前缀和。 |
| 59 | + |
| 60 | +<!-- tabs:start --> |
| 61 | + |
| 62 | +### **Python3** |
| 63 | + |
| 64 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 65 | + |
| 66 | +```python |
| 67 | +class Solution: |
| 68 | + def maximumSumScore(self, nums: List[int]) -> int: |
| 69 | + s = [0] + list(accumulate(nums)) |
| 70 | + return max(max(s[i + 1], s[-1] - s[i]) for i in range(len(nums))) |
| 71 | +``` |
| 72 | + |
| 73 | +### **Java** |
| 74 | + |
| 75 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 76 | + |
| 77 | +```java |
| 78 | +class Solution { |
| 79 | + public long maximumSumScore(int[] nums) { |
| 80 | + int n = nums.length; |
| 81 | + long[] s = new long[n + 1]; |
| 82 | + for (int i = 0; i < n; ++i) { |
| 83 | + s[i + 1] = s[i] + nums[i]; |
| 84 | + } |
| 85 | + long ans = Long.MIN_VALUE; |
| 86 | + for (int i = 0; i < n; ++i) { |
| 87 | + ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i])); |
| 88 | + } |
| 89 | + return ans; |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### **TypeScript** |
| 95 | + |
| 96 | +```ts |
| 97 | +function maximumSumScore(nums: number[]): number { |
| 98 | + const n = nums.length; |
| 99 | + let s = new Array(n + 1).fill(0); |
| 100 | + for (let i = 0; i < n; ++i) { |
| 101 | + s[i + 1] = s[i] + nums[i]; |
| 102 | + } |
| 103 | + let ans = -Infinity; |
| 104 | + for (let i = 0; i < n; ++i) { |
| 105 | + ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i])); |
| 106 | + } |
| 107 | + return ans; |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +### **C++** |
| 112 | + |
| 113 | +```cpp |
| 114 | +class Solution { |
| 115 | +public: |
| 116 | + long long maximumSumScore(vector<int>& nums) { |
| 117 | + int n = nums.size(); |
| 118 | + vector<long long> s(n + 1); |
| 119 | + for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i]; |
| 120 | + long long ans = INT_MIN; |
| 121 | + for (int i = 0; i < n; ++i) ans = max(ans, max(s[i + 1], s[n] - s[i])); |
| 122 | + return ans; |
| 123 | + } |
| 124 | +}; |
| 125 | +``` |
| 126 | +
|
| 127 | +### **Go** |
| 128 | +
|
| 129 | +```go |
| 130 | +func maximumSumScore(nums []int) int64 { |
| 131 | + n := len(nums) |
| 132 | + s := make([]int64, n+1) |
| 133 | + for i, v := range nums { |
| 134 | + s[i+1] = s[i] + int64(v) |
| 135 | + } |
| 136 | + var ans int64 = math.MinInt64 |
| 137 | + for i := 0; i < n; i++ { |
| 138 | + ans = max(ans, max(s[i+1], s[n]-s[i])) |
| 139 | + } |
| 140 | + return ans |
| 141 | +} |
| 142 | +
|
| 143 | +func max(a, b int64) int64 { |
| 144 | + if a > b { |
| 145 | + return a |
| 146 | + } |
| 147 | + return b |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +### **JavaScript** |
| 152 | + |
| 153 | +```js |
| 154 | +/** |
| 155 | + * @param {number[]} nums |
| 156 | + * @return {number} |
| 157 | + */ |
| 158 | +var maximumSumScore = function (nums) { |
| 159 | + const n = nums.length; |
| 160 | + let s = new Array(n + 1).fill(0); |
| 161 | + for (let i = 0; i < n; ++i) { |
| 162 | + s[i + 1] = s[i] + nums[i]; |
| 163 | + } |
| 164 | + let ans = -Infinity; |
| 165 | + for (let i = 0; i < n; ++i) { |
| 166 | + ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i])); |
| 167 | + } |
| 168 | + return ans; |
| 169 | +}; |
| 170 | +``` |
| 171 | + |
| 172 | +### **...** |
| 173 | + |
| 174 | +``` |
| 175 | +
|
| 176 | +``` |
| 177 | + |
| 178 | +<!-- tabs:end --> |
0 commit comments