Show problems as
Classic problem #486MediumArrayMathDynamic ProgrammingRecursionMinimaxGame TheoryZero-Sum Game

Predict the Winner

Real-world version: Bundle Claim Duel
Tracked in this browser only
Read Predict the Winner on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstProbability & Expectations · Dynamic Programming, animated step by step

Two auditors clear a row of document bundles. bundles[i] is the page count of the bundle at position i, and page counts are never negative.

The first auditor moves first and the two then alternate. A move takes the bundle at either end of the row that is left, adds its page count to that auditor's tally, and shortens the row. Play continues until no bundle remains. Both auditors know the whole row from the start and both play as well as possible for their own final tally.

Return true if the first auditor's final tally is at least as large as the second auditor's, and false otherwise. An equal pair of tallies counts in the first auditor's favour.

Examples

Example 1

Input
bundles = [3,9,1,2]
Output
true

With both auditors playing as well as they can, the first ends on 11 pages and the second on 4, so the first is not behind.

Example 2

Input
bundles = [4,8,3]
Output
false

Best play from both sides ends with the first auditor on 7 pages and the second on 8.

Constraints

  • 1 <= bundles.length <= 20
  • 0 <= bundles[i] <= 10^7

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def first_auditor_wins(bundles: list[int]) -> bool:
Java
public boolean firstAuditorWins(int[] bundles)