Welcome to Subscribe On Youtube
3788. Maximum Score of a Split
Description
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 ofnums[0] + nums[1] + ... + nums[i]. - Let
suffixMin(i)be the minimum value amongnums[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.
Example 1:
Input: nums = [10,-1,3,-4,-5]
Output: 17
Explanation:
The optimal split is at i = 2, score(2) = prefixSum(2) - suffixMin(2) = (10 + (-1) + 3) - (-5) = 17.
Example 2:
Input: nums = [-7,-5,3]
Output: -2
Explanation:
The optimal split is at i = 0, score(0) = prefixSum(0) - suffixMin(0) = (-7) - (-5) = -2.
Example 3:
Input: nums = [1,1]
Output: 0
Explanation:
The only valid split is at i = 0, score(0) = prefixSum(0) - suffixMin(0) = 1 - 1 = 0.
Constraints:
2 <= nums.length <= 105-109 <= nums[i] <= 109
Solutions
Solution 1: Prefix Sum + Enumeration
We first define an array $\textit{suf}$ of length $n$, where $\textit{suf}[i]$ represents the minimum value of the array $\textit{nums}$ from index $i$ to index $n - 1$. We can traverse the array $\textit{nums}$ from back to front to compute the array $\textit{suf}$.
Next, we define a variable $\textit{pre}$ to represent the prefix sum of the array $\textit{nums}$. We traverse the first $n - 1$ elements of the array $\textit{nums}$. For each index $i$, we add $\textit{nums}[i]$ to $\textit{pre}$ and calculate the split score $\textit{score}(i) = \textit{pre} - \textit{suf}[i + 1]$. We use a variable $\textit{ans}$ to maintain the maximum value among all split scores.
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.
-
class Solution { public long maximumScore(int[] nums) { int n = nums.length; long[] suf = new long[n]; suf[n - 1] = nums[n - 1]; for (int i = n - 2; i >= 0; --i) { suf[i] = Math.min(nums[i], suf[i + 1]); } long ans = Long.MIN_VALUE; long pre = 0; for (int i = 0; i < n - 1; ++i) { pre += nums[i]; ans = Math.max(ans, pre - suf[i + 1]); } return ans; } } -
class Solution { public: long long maximumScore(vector<int>& nums) { int n = nums.size(); vector<long long> suf(n); suf[n - 1] = nums[n - 1]; for (int i = n - 2; i >= 0; --i) { suf[i] = min((long long) nums[i], suf[i + 1]); } long long ans = LLONG_MIN; long long pre = 0; for (int i = 0; i < n - 1; ++i) { pre += nums[i]; ans = max(ans, pre - suf[i + 1]); } return ans; } }; -
class Solution: def maximumScore(self, nums: List[int]) -> int: n = len(nums) suf = [nums[-1]] * n for i in range(n - 2, -1, -1): suf[i] = min(nums[i], suf[i + 1]) ans = -inf pre = 0 for i in range(n - 1): pre += nums[i] ans = max(ans, pre - suf[i + 1]) return ans -
func maximumScore(nums []int) int64 { n := len(nums) suf := make([]int64, n) suf[n-1] = int64(nums[n-1]) for i := n - 2; i >= 0; i-- { suf[i] = min(int64(nums[i]), suf[i+1]) } var pre int64 = 0 var ans int64 = math.MinInt64 for i := 0; i < n-1; i++ { pre += int64(nums[i]) ans = max(ans, pre-suf[i+1]) } return ans } -
function maximumScore(nums: number[]): number { const n = nums.length; const suf: number[] = new Array(n); suf[n - 1] = nums[n - 1]; for (let i = n - 2; i >= 0; --i) { suf[i] = Math.min(nums[i], suf[i + 1]); } let ans = Number.NEGATIVE_INFINITY; let pre = 0; for (let i = 0; i < n - 1; ++i) { pre += nums[i]; ans = Math.max(ans, pre - suf[i + 1]); } return ans; }