Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6d7cee7

Browse files
committed
feat: add solutions to lc problem: No.2219
No.2219.Maximum Sum Score of Array
1 parent d9df7fe commit 6d7cee7

File tree

29 files changed

+1816
-24
lines changed

29 files changed

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /