Back to Month
HARD 24 Aug 2026 View on LeetCode

1872. Stone Game VIII

</> Solution

class Solution {
    public int stoneGameVIII(int[] stones) {
        int n = stones.length;
        long[] prefix = new long[n];
        prefix[0] = stones[0];
        for (int i = 1; i < n; i++) {
            prefix[i] = prefix[i - 1] + stones[i];
        }

        // dp represents the best score difference the current player can achieve
        // starting from the state where the "cut" is at index i (i.e., first i+1 stones merged)
        long dp = prefix[n - 1];
        for (int i = n - 2; i >= 1; i--) {
            dp = Math.max(dp, prefix[i] - dp);
        }

        return (int) dp;
    }
}

TIME COMPLEXITY

O(n)

SPACE COMPLEXITY

O(1)

TOPICS

Array Dynamic Programming Game Theory Prefix Sum