↓Skip to main content
  1. LeetCode/

LeetCode 3788: Maximum Score of a Split

·1 min· ·
LeetCode Medium Prefix-Sum Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-12-28

  • Submissions: Accepted (first try)

Approach
#

Split i scores prefixSum(i) - suffixMin(i + 1), and both can be precomputed (Prefix Sum Template 2):

  • Suffix minimum suf_min[i] = min(nums[i..n-1]), built right to left.
  • The prefix sum accumulates during the scan.

Numbers can be negative and so can the answer, so start from -inf.

My Solution
#

I originally named the two arrays backward (prefix_min was actually a prefix sum) and stored the suffix array reversed. Cleaned up:

class Solution:
    def maximumScore(self, nums: List[int]) -> int:
        n = len(nums)
        suf_min = nums[:]
        for i in range(n - 2, -1, -1):
            suf_min[i] = min(suf_min[i + 1], nums[i])
        best, prefix = -inf, 0
        for i in range(n - 1):
            prefix += nums[i]
            best = max(best, prefix - suf_min[i + 1])
        return best

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • The answer can be negative; do not start from 0.
  • i goes up to n - 2; the right side cannot be empty.

Interview Explanation
#

The score is the left prefix sum minus the right minimum. I build the suffix minimum right to left, then accumulate the prefix sum left to right and take the best split. Time O(n).

Related

LeetCode 1423: Maximum Points You Can Obtain from Cards
·1 min
LeetCode Medium Sliding-Window Prefix-Sum Array
LeetCode 1423 note: taking k cards from the ends leaves a contiguous middle of n - k cards; find its minimum sum with a fixed-size window
LeetCode 2391: Minimum Amount of Time to Collect Garbage
·2 mins
LeetCode Medium Prefix-Sum String Array
LeetCode 2391 note: pickup time is the total garbage count; each truck only drives to the last house with its type, using a prefix sum of travel
LeetCode 3494: Find the Minimum Amount of Time to Brew Potions
·2 mins
LeetCode Medium Simulation Prefix-Sum Array
LeetCode 3494 note: for each potion, push forward to get the last wizard’s finish time (everyone must be free), then subtract backward to get each wizard’s finish time
LeetCode 2874: Maximum Value of an Ordered Triplet II
·2 mins
LeetCode Medium Prefix-Sum Array
LeetCode 2874 note: fix the middle j and take the prefix maximum on the left and the suffix maximum on the right, or scan once tracking the maximum and the best difference
LeetCode 1248: Count Number of Nice Subarrays
·3 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 1248 note: “exactly k odd numbers” is not monotonic, so compute “at most k” minus “at most k - 1”; or use prefix sums with a hash map
LeetCode 3070: Count Submatrices with Top-Left Element and Sum Less Than k
·2 mins
LeetCode Medium Array Matrix Prefix-Sum
LeetCode 3070 note: submatrices containing the top-left corner are exactly the 2D prefix sums P[i][j]; count those <= k