[LeetCode] Maximum Sum Score of Array

2219. Maximum Sum Score of Array

You are given a 0-indexed integer array nums of length n.

The sum score of nums at an index i where 0 <= i < n is the maximum of:

  • The sum of the first i + 1 elements of nums.
  • The sum of the last n - i elements of nums.

Return the maximum sum score of nums at any index.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
long long maximumSumScore(vector<int>& nums) {
long long rsum = accumulate(begin(nums), end(nums), 0ll), lsum = 0, res = LLONG_MIN;
for(auto& n : nums) {
lsum += n;
res = max({res, lsum, rsum});
rsum -= n;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/06/PS/LeetCode/maximum-sum-score-of-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.