[LeetCode] Maximum Score of a Split

3788. Maximum Score of a Split

You are given an integer array nums of length n.

Choose an index i such that 0 <= i < n - 1.

For a chosen split index i:

  • Let prefixSum(i) be the sum of nums[0] + nums[1] + ... + nums[i].
  • Let suffixMin(i) be the minimum value among nums[i + 1], nums[i + 2], ..., nums[n - 1].

The score of a split at index i is defined as:

score(i) = prefixSum(i) - suffixMin(i)

Return an integer denoting the maximum score over all valid split indices.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
long long maximumScore(vector<int>& nums) {
long long n = nums.size(), res = LLONG_MIN, pre = 0;
vector<int> suf(n,nums.back());
for(int i = n - 2; i; i--) suf[i] = min(suf[i+1], nums[i]);
for(int i = 0; i < n - 1; i++) {
pre += nums[i];
res = max(res, pre - suf[i+1]);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/maximum-score-of-a-split/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.