Optimal First-Player Stones With a Growing Take Limit

Quick Overview

Optimal First-Player Stones With a Growing Take Limit. Positive stone piles are arranged in a row. Alice starts, then players alternate taking the first X remaining whole piles, with 1 <= X <= 2*M and M initially1.

Optimal First-Player Stones With a Growing Take Limit

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Positive stone piles are arranged in a row. Alice starts, then players alternate taking the first X remaining whole piles, with `1 <= X <= 2*M` and M initially 1. After each turn set M=max(M,X). Both maximize their own total. Return the most stones Alice can obtain under optimal play. ```cpp class Solution { public: int stoneGameII(vector<int>& piles); }; ``` ### Examples **Example 1:** ```text Input: piles = [4,1,2] Output: 5 Explanation: Taking the first two gives Alice5. Taking just4 lets Bob take the remaining3, so5 is better. ``` **Example 2:** ```text Input: piles = [1,1,1,1] Output: 2 Explanation: Alice can take two immediately. If she takes one, Bob takes two and leaves just one for her, still limiting her total to2. ``` ### Constraints - There are 1 to 100 piles. - Each pile contains an integer number of stones in `[1,10000]`. ### Contract considerations The objective is Alice's numeric total under optimal play, not a chosen move sequence. ```hint Read the boundary condition Does M reset when the turn changes, and may a player take piles from somewhere other than the front? ``` ### Boundary reminders The objective is Alice's numeric total under optimal play, not a chosen move sequence.

Overview: Optimal First-Player Stones With a Growing Take Limit. Positive stone piles are arranged in a row. Alice starts, then players alternate taking the first X remaining whole piles, with 1 <= X <= 2*M and M initially1.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Positive stone piles are arranged in a row. Alice starts, then players alternate taking the first X remaining whole piles, with 1 <= X <= 2*M and M initially 1. After each turn set M=max(M,X). Both maximize their own total. Return the most stones Alice can obtain under optimal play.

class Solution {
public:
    int stoneGameII(vector<int>& piles);
};

Examples

Example 1:

Input: piles = [4,1,2]
Output: 5
Explanation: Taking the first two gives Alice5. Taking just4 lets Bob take the remaining3, so5 is better.

Example 2:

Input: piles = [1,1,1,1]
Output: 2
Explanation: Alice can take two immediately. If she takes one, Bob takes two and leaves just one for her, still limiting her total to2.

Constraints

  • There are 1 to 100 piles.
  • Each pile contains an integer number of stones in [1,10000] .

Contract considerations

The objective is Alice's numeric total under optimal play, not a chosen move sequence.

Boundary reminders

The objective is Alice's numeric total under optimal play, not a chosen move sequence.