Optimal Matrix Game - Problem
Two players are playing an optimal strategy game with a row of numbers. Players take turns picking numbers from either end of the row. Each player tries to maximize their own score.
Given an array nums representing the row of numbers, return the maximum score that the first player can obtain, assuming both players play optimally.
Note: The first player always goes first. Both players have perfect information and play to maximize their own score.
Input & Output
Example 1 — Basic Game
$
Input:
nums = [1,5,2]
›
Output:
6
💡 Note:
First player picks 2, second player picks 5, first player picks 1. First player gets 2 + 1 = 3, but optimal play gives first player 6 total by picking 1 first, forcing second player to pick optimally from [5,2].
Example 2 — Larger Array
$
Input:
nums = [1,5,233,7]
›
Output:
240
💡 Note:
First player can pick 7, then after second player's optimal move, pick 233. Total: 233 + 7 = 240.
Example 3 — Equal Elements
$
Input:
nums = [3,3,3,3]
›
Output:
6
💡 Note:
Players alternate picking 3s. First player gets first and third picks: 3 + 3 = 6.
Constraints
- 1 ≤ nums.length ≤ 20
- 0 ≤ nums[i] ≤ 107
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code