Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an array points of size n and an integer m. There is another array gameScore of size n, where gameScore[i] represents the score achieved at the ith game. Initially, gameScore[i] == 0 for all i.
You start at index -1, which is outside the array (before the first position at index 0). You can make at most m moves. In each move, you can either:
points[i] to gameScore[i].points[i] to gameScore[i].Note that the index must always remain within the bounds of the array after the first move.
Return the maximum possible minimum value in gameScore after at most m moves.
Example 1:
Input: points = [2,4], m = 3
Output: 4
Explanation:
Initially, index i = -1 and gameScore = [0, 0].
| Move | Index | gameScore |
|---|---|---|
Increase i |
0 | [2, 0] |
Increase i |
1 | [2, 4] |
Decrease i |
0 | [4, 4] |
The minimum value in gameScore is 4, and this is the maximum possible minimum among all configurations. Hence, 4 is the output.
Example 2:
Input: points = [1,2,3], m = 5
Output: 2
Explanation:
Initially, index i = -1 and gameScore = [0, 0, 0].
| Move | Index | gameScore |
|---|---|---|
Increase i |
0 | [1, 0, 0] |
Increase i |
1 | [1, 2, 0] |
Decrease i |
0 | [2, 2, 0] |
Increase i |
1 | [2, 4, 0] |
Increase i |
2 | [2, 4, 3] |
The minimum value in gameScore is 2, and this is the maximum possible minimum among all configurations. Hence, 2 is the output.
Constraints:
2 <= n == points.length <= 5 * 1041 <= points[i] <= 1061 <= m <= 109Problem summary: You are given an array points of size n and an integer m. There is another array gameScore of size n, where gameScore[i] represents the score achieved at the ith game. Initially, gameScore[i] == 0 for all i. You start at index -1, which is outside the array (before the first position at index 0). You can make at most m moves. In each move, you can either: Increase the index by 1 and add points[i] to gameScore[i]. Decrease the index by 1 and add points[i] to gameScore[i]. Note that the index must always remain within the bounds of the array after the first move. Return the maximum possible minimum value in gameScore after at most m moves.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Greedy
[2,4] 3
[1,2,3] 5
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3449: Maximize the Minimum Game Score
class Solution {
public long maxScore(int[] points, int m) {
long l = 0;
long r = ((long) m + 1) / 2 * points[0] + 1;
while (l < r) {
final long mid = (l + r + 1) / 2;
if (isPossible(points, mid, m))
l = mid;
else
r = mid - 1;
}
return l;
}
// Returns true if it is possible to achieve the maximum minimum value `x`
// with `m` number of moves.
private boolean isPossible(int[] points, long minVal, long m) {
long moves = 0;
long prevMoves = 0; // to track remaining moves from the previous point
for (int i = 0; i < points.length; ++i) {
// ceil(minVal / point)
final long required = Math.max(0, (minVal + points[i] - 1) / points[i] - prevMoves);
if (required > 0) {
moves += 2L * required - 1;
prevMoves = required - 1;
} else if (i + 1 < points.length) {
moves += 1;
prevMoves = 0;
}
if (moves > m)
return false;
}
return true;
}
}
// Accepted solution for LeetCode #3449: Maximize the Minimum Game Score
package main
import (
"slices"
"sort"
)
// https://space.bilibili.com/206214
func maxScore(points []int, m int) int64 {
u1 := (m + 1) / 2 * slices.Min(points)
u2 := m / len(points) * slices.Max(points)
ans := sort.Search(min(u1, u2), func(low int) bool {
// 二分最小的不满足要求的 low+1,即可得到最大的满足要求的 low
low++
left := m
pre := 0
for i, p := range points {
k := (low-1)/p + 1 - pre // 还需要操作的次数
if i == len(points)-1 && k <= 0 { // 最后一个数已经满足要求
break
}
k = max(k, 1) // 至少要走 1 步
left -= k*2 - 1 // 左右横跳
if left < 0 {
return true
}
pre = k - 1 // 右边那个数已经操作 k-1 次
}
return false
})
return int64(ans)
}
# Accepted solution for LeetCode #3449: Maximize the Minimum Game Score
class Solution:
def maxScore(self, points: list[int], m: int) -> int:
def isPossible(minVal: int, m: int) -> bool:
"""
Returns True if it is possible to achieve the maximum minimum value `x`
with `m` number of moves.
"""
moves = 0
prevMoves = 0 # to track remaining moves from the previous point
for i, point in enumerate(points):
required = (minVal + point - 1) // point # ceil(minVal / point)
required = max(0, required - prevMoves)
if required > 0:
moves += 2 * required - 1
prevMoves = required - 1
elif i + 1 < len(points):
moves += 1
prevMoves = 0
if moves > m:
return False
return True
l = 0
r = (m + 1) // 2 * points[0] + 1
while l < r:
mid = (l + r + 1) // 2
if isPossible(mid, m):
l = mid
else:
r = mid - 1
return l
// Accepted solution for LeetCode #3449: Maximize the Minimum Game Score
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3449: Maximize the Minimum Game Score
// class Solution {
// public long maxScore(int[] points, int m) {
// long l = 0;
// long r = ((long) m + 1) / 2 * points[0] + 1;
//
// while (l < r) {
// final long mid = (l + r + 1) / 2;
// if (isPossible(points, mid, m))
// l = mid;
// else
// r = mid - 1;
// }
//
// return l;
// }
//
// // Returns true if it is possible to achieve the maximum minimum value `x`
// // with `m` number of moves.
// private boolean isPossible(int[] points, long minVal, long m) {
// long moves = 0;
// long prevMoves = 0; // to track remaining moves from the previous point
// for (int i = 0; i < points.length; ++i) {
// // ceil(minVal / point)
// final long required = Math.max(0, (minVal + points[i] - 1) / points[i] - prevMoves);
// if (required > 0) {
// moves += 2L * required - 1;
// prevMoves = required - 1;
// } else if (i + 1 < points.length) {
// moves += 1;
// prevMoves = 0;
// }
// if (moves > m)
// return false;
// }
// return true;
// }
// }
// Accepted solution for LeetCode #3449: Maximize the Minimum Game Score
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3449: Maximize the Minimum Game Score
// class Solution {
// public long maxScore(int[] points, int m) {
// long l = 0;
// long r = ((long) m + 1) / 2 * points[0] + 1;
//
// while (l < r) {
// final long mid = (l + r + 1) / 2;
// if (isPossible(points, mid, m))
// l = mid;
// else
// r = mid - 1;
// }
//
// return l;
// }
//
// // Returns true if it is possible to achieve the maximum minimum value `x`
// // with `m` number of moves.
// private boolean isPossible(int[] points, long minVal, long m) {
// long moves = 0;
// long prevMoves = 0; // to track remaining moves from the previous point
// for (int i = 0; i < points.length; ++i) {
// // ceil(minVal / point)
// final long required = Math.max(0, (minVal + points[i] - 1) / points[i] - prevMoves);
// if (required > 0) {
// moves += 2L * required - 1;
// prevMoves = required - 1;
// } else if (i + 1 < points.length) {
// moves += 1;
// prevMoves = 0;
// }
// if (moves > m)
// return false;
// }
// return true;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.