Maximize the Minimum Game Score Problem
Description
LeetCode Problem 3449.
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 i^th 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]togameScore[i]. - Decrease the index by 1 and add
points[i]togameScore[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 * 10^41 <= points[i] <= 10^61 <= m <= 10^9
Solution
Approach: Binary Search on the Answer + Greedy
If a minimum score x can be reached, any smaller one can too, so we binary search on x. For a candidate x, index i must be visited at least ceil(x / points[i]) times, and we need to know whether m moves are enough.
Sweep from left to right, never going further right than needed. When we first step onto index i, we may already have pre visits there, left over from bouncing back from index i + 1. If we still need k = ceil(x / points[i]) - pre more visits, we get them by stepping i -> i + 1 -> i repeatedly: each extra visit costs two moves and also gives i + 1 one visit for free. Entering i costs one move, so index i costs 2k - 1 moves in total, where k is at least 1 for every index except possibly the last (we have to walk past it anyway), and the free visits we pass on to i + 1 are k - 1. At the last index, if no more visits are needed we stop without moving further.
If the total number of moves stays within m, x is feasible. The answer is at most m * 10^6 < 10^15; if even x = 1 is not feasible, the answer is 0.
Complexity: O(n log(m * max(points))) time and O(1) extra space.
class Solution:
def maxScore(self, points: List[int], m: int) -> int:
n = len(points)
def check(low: int) -> bool:
rem = m
pre = 0
for i, p in enumerate(points):
k = (low + p - 1) // p - pre
if i == n - 1 and k <= 0:
break
k = max(k, 1)
rem -= 2 * k - 1
if rem < 0:
return False
pre = k - 1
return True
l, r = 0, 10 ** 15 + 1
while l < r:
mid = (l + r + 1) >> 1
if check(mid):
l = mid
else:
r = mid - 1
return l