Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 426b9e9

Browse files
authored
feat: add solutions to lc problem: No.2219 (doocs#3976)
No.2219.Maximum Sum Score of Array
1 parent f48ca99 commit 426b9e9

File tree

9 files changed

+196
-110
lines changed

9 files changed

+196
-110
lines changed

solution/2200-2299/2219.Maximum Sum Score of Array/README.md

Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ nums 可取得的最大总分是 -3 。
7070

7171
<!-- solution:start -->
7272

73-
### 方法一
73+
### 方法一:前缀和
74+
75+
我们可以使用两个变量 $l$ 和 $r$ 分别表示数组的前缀和和后缀和,初始时 $l = 0$, $r = \sum_{i=0}^{n-1} \textit{nums}[i]$。
76+
77+
接下来,我们遍历数组 $\textit{nums}$,对于每个元素 $x$,我们将 $l$ 增加 $x$,并更新答案 $\textit{ans} = \max(\textit{ans}, l, r)$,然后将 $r$ 减少 $x$。
78+
79+
遍历结束后,返回答案 $\textit{ans}$ 即可。
80+
81+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
7482

7583
<!-- tabs:start -->
7684

@@ -79,23 +87,29 @@ nums 可取得的最大总分是 -3 。
7987
```python
8088
class Solution:
8189
def maximumSumScore(self, nums: List[int]) -> int:
82-
s = [0] + list(accumulate(nums))
83-
return max(max(s[i + 1], s[-1] - s[i]) for i in range(len(nums)))
90+
l, r = 0, sum(nums)
91+
ans = -inf
92+
for x in nums:
93+
l += x
94+
ans = max(ans, l, r)
95+
r -= x
96+
return ans
8497
```
8598

8699
#### Java
87100

88101
```java
89102
class Solution {
90103
public long maximumSumScore(int[] nums) {
91-
int n = nums.length;
92-
long[] s = new long[n + 1];
93-
for (int i = 0; i < n; ++i) {
94-
s[i + 1] = s[i] + nums[i];
104+
long l = 0, r = 0;
105+
for (int x : nums) {
106+
r += x;
95107
}
96108
long ans = Long.MIN_VALUE;
97-
for (int i = 0; i < n; ++i) {
98-
ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i]));
109+
for (int x : nums) {
110+
l += x;
111+
ans = Math.max(ans, Math.max(l, r));
112+
r -= x;
99113
}
100114
return ans;
101115
}
@@ -108,11 +122,13 @@ class Solution {
108122
class Solution {
109123
public:
110124
long long maximumSumScore(vector<int>& nums) {
111-
int n = nums.size();
112-
vector<long long> s(n + 1);
113-
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i];
114-
long long ans = INT_MIN;
115-
for (int i = 0; i < n; ++i) ans = max(ans, max(s[i + 1], s[n] - s[i]));
125+
long long l = 0, r = accumulate(nums.begin(), nums.end(), 0LL);
126+
long long ans = -1e18;
127+
for (int x : nums) {
128+
l += x;
129+
ans = max({ans, l, r});
130+
r -= x;
131+
}
116132
return ans;
117133
}
118134
};
@@ -122,36 +138,54 @@ public:
122138
123139
```go
124140
func maximumSumScore(nums []int) int64 {
125-
n := len(nums)
126-
s := make([]int64, n+1)
127-
for i, v := range nums {
128-
s[i+1] = s[i] + int64(v)
141+
l, r := 0, 0
142+
for _, x := range nums {
143+
r += x
129144
}
130-
var ans int64 = math.MinInt64
131-
for i := 0; i < n; i++ {
132-
ans = max(ans, max(s[i+1], s[n]-s[i]))
145+
ans := math.MinInt64
146+
for _, x := range nums {
147+
l += x
148+
ans = max(ans, max(l, r))
149+
r -= x
133150
}
134-
return ans
151+
return int64(ans)
135152
}
136153
```
137154

138155
#### TypeScript
139156

140157
```ts
141158
function maximumSumScore(nums: number[]): number {
142-
const n = nums.length;
143-
let s = new Array(n + 1).fill(0);
144-
for (let i = 0; i < n; ++i) {
145-
s[i + 1] = s[i] + nums[i];
146-
}
159+
let l = 0;
160+
let r = nums.reduce((a, b) => a + b, 0);
147161
let ans = -Infinity;
148-
for (let i = 0; i < n; ++i) {
149-
ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i]));
162+
for (const x of nums) {
163+
l += x;
164+
ans = Math.max(ans, l, r);
165+
r -= x;
150166
}
151167
return ans;
152168
}
153169
```
154170

171+
#### Rust
172+
173+
```rust
174+
impl Solution {
175+
pub fn maximum_sum_score(nums: Vec<i32>) -> i64 {
176+
let mut l = 0;
177+
let mut r: i64 = nums.iter().map(|&x| x as i64).sum();
178+
let mut ans = std::i64::MIN;
179+
for &x in &nums {
180+
l += x as i64;
181+
ans = ans.max(l).max(r);
182+
r -= x as i64;
183+
}
184+
ans
185+
}
186+
}
187+
```
188+
155189
#### JavaScript
156190

157191
```js
@@ -160,14 +194,13 @@ function maximumSumScore(nums: number[]): number {
160194
* @return {number}
161195
*/
162196
var maximumSumScore = function (nums) {
163-
const n = nums.length;
164-
let s = new Array(n + 1).fill(0);
165-
for (let i = 0; i < n; ++i) {
166-
s[i + 1] = s[i] + nums[i];
167-
}
197+
let l = 0;
198+
let r = nums.reduce((a, b) => a + b, 0);
168199
let ans = -Infinity;
169-
for (let i = 0; i < n; ++i) {
170-
ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i]));
200+
for (const x of nums) {
201+
l += x;
202+
ans = Math.max(ans, l, r);
203+
r -= x;
171204
}
172205
return ans;
173206
};

solution/2200-2299/2219.Maximum Sum Score of Array/README_EN.md

Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ The maximum sum score of nums is -3.
6868

6969
<!-- solution:start -->
7070

71-
### Solution 1
71+
### Solution 1: Prefix Sum
72+
73+
We can use two variables $l$ and $r$ to represent the prefix sum and suffix sum of the array, respectively. Initially, $l = 0$ and $r = \sum_{i=0}^{n-1} \textit{nums}[i]$.
74+
75+
Next, we traverse the array $\textit{nums}$. For each element $x$, we add $x$ to $l$ and update the answer $\textit{ans} = \max(\textit{ans}, l, r)$, then subtract $x$ from $r$.
76+
77+
After the traversal, return the answer $\textit{ans}$.
78+
79+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
7280

7381
<!-- tabs:start -->
7482

@@ -77,23 +85,29 @@ The maximum sum score of nums is -3.
7785
```python
7886
class Solution:
7987
def maximumSumScore(self, nums: List[int]) -> int:
80-
s = [0] + list(accumulate(nums))
81-
return max(max(s[i + 1], s[-1] - s[i]) for i in range(len(nums)))
88+
l, r = 0, sum(nums)
89+
ans = -inf
90+
for x in nums:
91+
l += x
92+
ans = max(ans, l, r)
93+
r -= x
94+
return ans
8295
```
8396

8497
#### Java
8598

8699
```java
87100
class Solution {
88101
public long maximumSumScore(int[] nums) {
89-
int n = nums.length;
90-
long[] s = new long[n + 1];
91-
for (int i = 0; i < n; ++i) {
92-
s[i + 1] = s[i] + nums[i];
102+
long l = 0, r = 0;
103+
for (int x : nums) {
104+
r += x;
93105
}
94106
long ans = Long.MIN_VALUE;
95-
for (int i = 0; i < n; ++i) {
96-
ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i]));
107+
for (int x : nums) {
108+
l += x;
109+
ans = Math.max(ans, Math.max(l, r));
110+
r -= x;
97111
}
98112
return ans;
99113
}
@@ -106,11 +120,13 @@ class Solution {
106120
class Solution {
107121
public:
108122
long long maximumSumScore(vector<int>& nums) {
109-
int n = nums.size();
110-
vector<long long> s(n + 1);
111-
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i];
112-
long long ans = INT_MIN;
113-
for (int i = 0; i < n; ++i) ans = max(ans, max(s[i + 1], s[n] - s[i]));
123+
long long l = 0, r = accumulate(nums.begin(), nums.end(), 0LL);
124+
long long ans = -1e18;
125+
for (int x : nums) {
126+
l += x;
127+
ans = max({ans, l, r});
128+
r -= x;
129+
}
114130
return ans;
115131
}
116132
};
@@ -120,36 +136,54 @@ public:
120136
121137
```go
122138
func maximumSumScore(nums []int) int64 {
123-
n := len(nums)
124-
s := make([]int64, n+1)
125-
for i, v := range nums {
126-
s[i+1] = s[i] + int64(v)
139+
l, r := 0, 0
140+
for _, x := range nums {
141+
r += x
127142
}
128-
var ans int64 = math.MinInt64
129-
for i := 0; i < n; i++ {
130-
ans = max(ans, max(s[i+1], s[n]-s[i]))
143+
ans := math.MinInt64
144+
for _, x := range nums {
145+
l += x
146+
ans = max(ans, max(l, r))
147+
r -= x
131148
}
132-
return ans
149+
return int64(ans)
133150
}
134151
```
135152

136153
#### TypeScript
137154

138155
```ts
139156
function maximumSumScore(nums: number[]): number {
140-
const n = nums.length;
141-
let s = new Array(n + 1).fill(0);
142-
for (let i = 0; i < n; ++i) {
143-
s[i + 1] = s[i] + nums[i];
144-
}
157+
let l = 0;
158+
let r = nums.reduce((a, b) => a + b, 0);
145159
let ans = -Infinity;
146-
for (let i = 0; i < n; ++i) {
147-
ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i]));
160+
for (const x of nums) {
161+
l += x;
162+
ans = Math.max(ans, l, r);
163+
r -= x;
148164
}
149165
return ans;
150166
}
151167
```
152168

169+
#### Rust
170+
171+
```rust
172+
impl Solution {
173+
pub fn maximum_sum_score(nums: Vec<i32>) -> i64 {
174+
let mut l = 0;
175+
let mut r: i64 = nums.iter().map(|&x| x as i64).sum();
176+
let mut ans = std::i64::MIN;
177+
for &x in &nums {
178+
l += x as i64;
179+
ans = ans.max(l).max(r);
180+
r -= x as i64;
181+
}
182+
ans
183+
}
184+
}
185+
```
186+
153187
#### JavaScript
154188

155189
```js
@@ -158,14 +192,13 @@ function maximumSumScore(nums: number[]): number {
158192
* @return {number}
159193
*/
160194
var maximumSumScore = function (nums) {
161-
const n = nums.length;
162-
let s = new Array(n + 1).fill(0);
163-
for (let i = 0; i < n; ++i) {
164-
s[i + 1] = s[i] + nums[i];
165-
}
195+
let l = 0;
196+
let r = nums.reduce((a, b) => a + b, 0);
166197
let ans = -Infinity;
167-
for (let i = 0; i < n; ++i) {
168-
ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i]));
198+
for (const x of nums) {
199+
l += x;
200+
ans = Math.max(ans, l, r);
201+
r -= x;
169202
}
170203
return ans;
171204
};
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
class Solution {
22
public:
33
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]));
4+
long long l = 0, r = accumulate(nums.begin(), nums.end(), 0LL);
5+
long long ans = -1e18;
6+
for (int x : nums) {
7+
l += x;
8+
ans = max({ans, l, r});
9+
r -= x;
10+
}
911
return ans;
1012
}
11-
};
13+
};
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
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)
2+
l, r := 0, 0
3+
for _, x := range nums {
4+
r += x
65
}
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]))
6+
ans := math.MinInt64
7+
for _, x := range nums {
8+
l += x
9+
ans = max(ans, max(l, r))
10+
r -= x
1011
}
11-
return ans
12-
}
12+
return int64(ans)
13+
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
class Solution {
22
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];
3+
long l = 0, r = 0;
4+
for (int x : nums) {
5+
r += x;
76
}
87
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]));
8+
for (int x : nums) {
9+
l += x;
10+
ans = Math.max(ans, Math.max(l, r));
11+
r -= x;
1112
}
1213
return ans;
1314
}
14-
}
15+
}

0 commit comments

Comments
 (0)